foreach my $name (@array){ print "$name "; if($name eq 'joby'){ shift(@array); print "deleted\n"; next; } print "ok\n"; }
Your problem is that you're modifying the array you're iterating over. Just don't do that with a foreach loop :)
(This issue comes up from time to time, so if you search this site you should find rather extensive related discussion... (e.g. self-feeding infinite loop))
In reply to Re: Trouble with loops
by almut
in thread Trouble with loops
by jobybett
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |