for loop

F

Fatted

Consider code snippit:

my @array = qw(first second third fourth);

for(@array)
{
print $_."\n";
}

Which prints to stdout:
first
second
third
fourth

Now try:

my @array = qw(first second third fourth);
my $i;

for($i = 0, @array, $i++)
{
print $_."\n";
}

This prints:
1
first
second
third
fourth
0

What are the 1 and 0 values?
 
M

Mark Clements

Fatted wrote:

for($i = 0, @array, $i++)
{
print $_."\n";
}

This prints:
1
first
second
third
fourth
0

You have the syntax for "for" incorrect (OK - it's "correct", but isn't what you want to do
here). Try

for(my $i = 0 ; $i < $#array ; $i++){

note ; instead of ,
also, @array replaced by $i < $#array. Unless you are modifying @array (not a good variable
name, by the way), then it will always be true in this case.

The reason your version works at all is due to how for (or foreach - they are synonymous )
processes arguments when they consist of just a list, and I'm feeling a bit to lazy to work it
out properly to explain it.

Mark

Mark
 
T

Tore Aursand

my @array = qw(first second third fourth);
my $i;

for($i = 0, @array, $i++)
{
print $_."\n";
}

This prints:
1
first
second
third
fourth
0

What are the 1 and 0 values?

What are you actually trying to do? That 'for' loop looks a little weird
to me, but Perl does excactly what you tells it to;

You tell Perl to loop through a list containing:

1. $i = 0
2. The elements in @array
3. $i++

Is that what you _really_ want Perl to do?
 
J

Joe Smith

Fatted said:
Consider code snippit:
my @array = qw(first second third fourth);
my $i;
for($i = 0, @array, $i++) {print $_."\n";}

The C-style for() loop requires semicolons, not commas.

for ($i = 0; $i < @array; $i++) { print "$array[$i]\n"; }
This prints:
1
first
second
third
fourth
0

What are the 1 and 0 values?

One is from $i=0, the other is from $i++.
for($i = 0, @array, $i++) {print $_."\n";}

That is the same as
$i = 0;
for $_ ($i, 'first', 'second', 'third', 'fourth', $i) {...}
except that the post-increment is done at an unexpected place.

-Joe
 
J

J. Gleixner

Fatted said:
my @array = qw(first second third fourth);
my $i;

for($i = 0, @array, $i++)
{
print $_."\n";
}

Others have pointed out the ',', error. Just wanted to mention that
you'll find the following more common, in perl:

foreach my $i ( 0 .. $#array )
{
print "$i $array[$i]\n";
}
 
T

Tore Aursand

Others have pointed out the ',', error. Just wanted to mention that
you'll find the following more common, in perl:

foreach my $i ( 0 .. $#array )
{
print "$i $array[$i]\n";
}

Or - shorter;

print "$_ $array[$_]\n" for ( 0..$#array );
 
R

Richard Morse

Mark Clements said:
You have the syntax for "for" incorrect (OK - it's "correct", but isn't what
you want to do
here). Try

for(my $i = 0 ; $i < $#array ; $i++){

note ; instead of ,
also, @array replaced by $i < $#array. Unless you are modifying @array (not a
good variable
name, by the way), then it will always be true in this case.

Shouldn't that be either:

$i <= $#array

or

$i < @array

(or, and I know I will be castigated to kingdom come for this, but my
preferred version is:

$i < scalar(@array)

)

Ricky
 
M

Mark Clements

Richard said:
Shouldn't that be either:

$i <= $#array
or
$i < @array
er - yes. off by one, and not for the first time :(
(or, and I know I will be castigated to kingdom come for this, but my
preferred version is:

$i < scalar(@array)
does it for me: nothing wrong with adding clarity by being explicit.

Mark
 
G

Graham Wood

<SNIP>
I think what is happening here, to quote from perldoc perlop, is " list
operators tend to gobble up all arguments that follow, and then act like
a simple TERM with regard to the preceding expression. " So the list in
your for loop ends up being $i++ then the elements in @array then the
$i=0 which is then treated as one expression with values:
1, first, second, third, fourth, 0

Can anyone confirm this theory?

Graham
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top