in reply to For vs. While
for (@array) loops until the number of iterations is greater than the number of elements in the array. When you reached the 3rd iteration of your for loop, @_ only has two elements (because of shift and pop), so the loop exits.
sub f1 { while (@_) { print(shift); } print("\n"); } sub f2 { for (@_) { print(shift); } print("\n"); } sub f3 { for (map $_, @_) { print(shift); } print("\n"); } f1(map/./g, Japh); # Japh f2(map/./g, Japh); # Ja f3(map/./g, Japh); # Japh
f2 iterates over the elements of @_.
f3 iterates over a premade list on the stack, so it doesn't matter that @_ changes.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: For vs. While
by Andrew_Levenson (Hermit) on Dec 06, 2006 at 00:54 UTC | |
by ikegami (Patriarch) on Dec 06, 2006 at 01:10 UTC |