in reply to extracting splices with a for-loop and splice()

I would generally be inclined to use
@ar = grep { my $all_matched=1; for my $p (@patterns) { last unless $all_matched = /$p/; } $all_matched; } @ar;
but if reconstructing the array this way was a memory concern, I'd walk backward through the array so that anything I removed would not affect the indexes of elements I have yet to examine:
sub filter_stuff { my $stuff = shift; my @patterns = @_; # Backwards iteration trick: # use negative of negative loop variable as index for my $i (-$#$stuff..0) { for (@patterns) { splice(@$stuff, -$i, 1) && last unless $stuff->[-$i] =~ /$_ +/; } } }
Update: The non-tricky way of walking backward through an array, of course, is
for (my $i=$#$stuff; $i >= 0; --$i) { #use $i as index }
I just wanted to avoid the C-style loop, for religious reasons.

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
subtle beauty.
by Anonymous Monk on Jan 28, 2004 at 19:32 UTC
    Latter one is indeed an outstanding idea.