Write a loop

R

Ron

Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}

if ($size_file2 > 1) {
$Num_Files = 2;
}

if ($size_file3 > 1) {
$Num_Files = 3;
}


if ($size_file4 > 1) {
$Num_Files = 4;
}

if ($size_file5 > 1) {
$Num_Files = 5;
}


if ($size_file6 > 1) {
$Num_Files = 6;
}


if ($size_file7 > 1) {
$Num_Files = 7;
}


if ($size_file8 > 1) {
$Num_Files = 8;
}


if ($size_file9 > 1) {
$Num_Files = 9;
}

Thanks,
Ron
 
J

Jürgen Exner

Ron said:
Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}
if ($size_file2 > 1) {
$Num_Files = 2;
}
if ($size_file3 > 1) {
$Num_Files = 3;
}
if ($size_file4 > 1) {
$Num_Files = 4;
}
if ($size_file5 > 1) {
$Num_Files = 5;
}
if ($size_file6 > 1) {
$Num_Files = 6;
}
if ($size_file7 > 1) {
$Num_Files = 7;
}
if ($size_file8 > 1) {
$Num_Files = 8;
}
if ($size_file9 > 1) {
$Num_Files = 9;
}

Use a more suitable datastructe.
If you enumerate scalars like this then you should really use an array
instead (the other option would have been a hash).

Untested, just to give you an idea:

my @size_file = <<<Filled with whatever you want>>>;
for (1..9) {
if ($size_file[$_] >1) {$Num_Files = $_;}

jue
 
S

Shawn Corey

Hi,

Store the file sizes in an array?

for( my $i = $Number_of_files; $i; $i -- ){
if( $size_file[ $i ] > 1 ){
$Num_Files = $i;
last;
}
}
 
R

Ron

Thanks Jürgen,

Tried this code but I get a server 500 error. Can I use zero for my
@size_file = 0 ?

my @size_file = said:
for (1..9) {
if ($size_file[$_] >1) {$Num_Files = $_;}


Jürgen Exner said:
Ron said:
Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}
if ($size_file2 > 1) {
$Num_Files = 2;
}
if ($size_file3 > 1) {
$Num_Files = 3;
}
if ($size_file4 > 1) {
$Num_Files = 4;
}
if ($size_file5 > 1) {
$Num_Files = 5;
}
if ($size_file6 > 1) {
$Num_Files = 6;
}
if ($size_file7 > 1) {
$Num_Files = 7;
}
if ($size_file8 > 1) {
$Num_Files = 8;
}
if ($size_file9 > 1) {
$Num_Files = 9;
}

Use a more suitable datastructe.
If you enumerate scalars like this then you should really use an array
instead (the other option would have been a hash).

Untested, just to give you an idea:

my @size_file = <<<Filled with whatever you want>>>;
for (1..9) {
if ($size_file[$_] >1) {$Num_Files = $_;}

jue
 
J

JS Bangs

Ron sikyal:
Thanks Jürgen,

Tried this code but I get a server 500 error. Can I use zero for my
@size_file = 0 ?

Um, the perl statemenet @size_file = 0 makes very little sense. Do you
understand what an array is, and how you should assign to it?
my @size_file = said:
for (1..9) {
if ($size_file[$_] >1) {$Num_Files = $_;}

This code is missing a trailing '}'. Try:

my @size_file = ( <<<< list of file sizes >>>> );
my $num_files = 0;
for (1 .. 9) {
if ($size_file[$_] > 1) {
$num_files++;
}
}

Jesse S. Bangs (e-mail address removed)
http://students.washington.edu/jaspax/
http://students.washington.edu/jaspax/blog
 
J

Jürgen Exner

[Please do not top-post; rearranged into chronological order]
[Please reduce the quoted text to a reasonable amount]
Jürgen Exner said:
Ron said:
Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}
if ($size_file2 > 1) {
$Num_Files = 2;
}
if ($size_file3 > 1) {
$Num_Files = 3;
[...]
Use a more suitable datastructe.
If you enumerate scalars like this then you should really use an
array instead (the other option would have been a hash).

Untested, just to give you an idea:

my @size_file = <<<Filled with whatever you want>>>;
for (1..9) {
if ($size_file[$_] >1) {$Num_Files = $_;}
Tried this code but I get a server 500 error.

That is not a perl error message. Please see 'perldoc -q 500' about what to
do.
Can I use zero for my
@size_file = 0 ?

Well, it as poor style to assign a scalar to an array.
This is better written as
@size_file = (0);
because then it is clear that you are assigning a one-element list to the
array.

But why do you want to assign only one value to the array when in your
original code you had 9 values?

jue
 
J

John W. Krahn

Ron said:
Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}
if ($size_file2 > 1) {
$Num_Files = 2;
}
if ($size_file3 > 1) {
$Num_Files = 3;
}
if ($size_file4 > 1) {
$Num_Files = 4;
}
if ($size_file5 > 1) {
$Num_Files = 5;
}
if ($size_file6 > 1) {
$Num_Files = 6;
}
if ($size_file7 > 1) {
$Num_Files = 7;
}
if ($size_file8 > 1) {
$Num_Files = 8;
}
if ($size_file9 > 1) {
$Num_Files = 9;
}

Sure, no problem.

my $Num_Files = 0;
for ( $size_file, $size_file2, $size_file3,
$size_file4, $size_file5, $size_file6,
$size_file7, $size_file8, $size_file9 ) {

last unless $_ > 1;
$Num_Files++;
}


John
 
J

John W. Krahn

Ron said:
Would you show me how I can write this as a loop?

#Get the number of files uploaded
my $Num_Files = 0;
if ($size_file > 1) {
$Num_Files = 1;
}
if ($size_file2 > 1) {
$Num_Files = 2;
}
if ($size_file3 > 1) {
$Num_Files = 3;
}
if ($size_file4 > 1) {
$Num_Files = 4;
}
if ($size_file5 > 1) {
$Num_Files = 5;
}
if ($size_file6 > 1) {
$Num_Files = 6;
}
if ($size_file7 > 1) {
$Num_Files = 7;
}
if ($size_file8 > 1) {
$Num_Files = 8;
}
if ($size_file9 > 1) {
$Num_Files = 9;
}

Sure, no problem.

my @files = ( $size_file9, $size_file8, $size_file7,
$size_file6, $size_file5, $size_file4,
$size_file3, $size_file2, $size_file );
my $Num_Files = @files;
for ( @files ) {
last if $_ > 1;
$Num_Files--;
}


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,303
Latest member
Ketonara

Latest Threads

Top