in reply to How foreach loops decide what to iterate through
I notice that the following code runs forever:Adding a print inside the loop might shine some light on it:
foreach (@a) { push @a, 1; print scalar @a, "\n" }
Prints:
Apparently, the push causes the array to grow by one element, somehow triggering the loop to iterate again.2 3 4 5 ...
I found this in the docs for Foreach Loops:
If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.
Now I am curious as to what perl is doing internally. Can any monks offer a deeper explanation?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How foreach loops decide what to iterate through (push array inside foreach)
by ikegami (Patriarch) on Feb 14, 2009 at 03:14 UTC | |
|
Re^2: How foreach loops decide what to iterate through (push array inside foreach)
by jethro (Monsignor) on Feb 14, 2009 at 03:33 UTC | |
by ikegami (Patriarch) on Feb 14, 2009 at 03:51 UTC | |
by jethro (Monsignor) on Feb 14, 2009 at 05:25 UTC | |
by ikegami (Patriarch) on Feb 14, 2009 at 05:37 UTC |