in reply to Question Regarding remove elements from a list

If each existing foreach removes more elements than it keeps, the order is not sorted, and memory is not an issue, then

my @tmp; while (my $elem = pop @array) { # or s/pop/shift/ # do stuff if ($keep) { push @tmp, $elem; } } @array = @tmp;
Another method is to use an array index instead of foreach, and splice:
my $i=0; while ($i < @array) { if (keep($array[$i]) { print "keeping $array[$i]\n"; $i++; } else { my $x = splice(@array,$i,1); print "discarding $x\n"; } }