makes $y an alias for one element of @a each time through the loop. Writingfor my $y ( @a ) {...}
is identical, but 4 chars longer to type.foreach my $y ( @a ) {...}
On the other hand,
would behave similarly, except $y is not an alias for elements of @a, but a copy. And usingforeach ( $x=0; $x < $#a; $x++ ) {my $y=$a[$x];...}
would not change the behavior here either.for ( $x=0; $x < $#a; $x++ ) {my $y=$a[$x];...}
Which form the compiler chooses is based on the argument list to for/foreach, and not the name itself!
I hope that helps :|
Update: I wish I'd have said it more like hv did below. Maybe he'll volunteer to edit for me in the future ;)
A for loop and a foreach loop are slightly different.I attempted (but failed) to say that the OP seemed to understand them as different entities. The only difference is what follows the keyword, and not the keyword itself.
-QM
--
Quantum Mechanics: The dreams stuff is made of
In reply to Re^3: Closures & aliases
by QM
in thread Closures & aliases
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |