in reply to Is it safe to append to the array you are iterating over
The question then is is this violation of the general prohibition safe or was I luckyNow I am confused. What do you expect,
What I think is safe, is this:
foreach(my @temp = @arr) { push @arr, 'd' if $_ eq 'a'; push @arr, 'e' if $_ eq 'b'; push @arr, 'f' if $_ eq 'c'; print $_; }
for(my $i = 0; $i < @arr; $i++) { local $_ = $arr[$i]; push @arr, 'd' if $_ eq 'a'; push @arr, 'e' if $_ eq 'b'; push @arr, 'f' if $_ eq 'c'; print $_; }
but it doesn't loop even once. I think that's a bit weird, sinceforeach(() = @arr) { print; }
actually sets $x to the size of the array.$x = () = @arr;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it safe to append to the array you are iterating over
by DrWhy (Chaplain) on Mar 14, 2013 at 19:11 UTC |