According to perldoc perlsyn:
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.
So, it seems to me, you probably don't want to be adding
or removing elements while within a for loop. Was that
your question?
For example, I just tested this w/ shift. I looped through
an array using foreach and did a shift part of the way
through:
my @arr = qw/foo bar baz quack/;
for (@arr) {
print $_, "\n";
shift @arr if $_ eq "bar";
}
Here's the output:
foo
bar
quack
So the shift shifted off "foo", which messed up the
internal loop counter, which means that "baz" didn't
get displayed. So this probably isn't the best idea. |