in reply to Question Regarding remove elements from a list
Another method is to use an array index instead of foreach, and splice:my @tmp; while (my $elem = pop @array) { # or s/pop/shift/ # do stuff if ($keep) { push @tmp, $elem; } } @array = @tmp;
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"; } }
|
|---|