in reply to extracting splices with a for-loop and splice()
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:@ar = grep { my $all_matched=1; for my $p (@patterns) { last unless $all_matched = /$p/; } $all_matched; } @ar;
Update: The non-tricky way of walking backward through an array, of course, issub 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] =~ /$_ +/; } } }
I just wanted to avoid the C-style loop, for religious reasons.for (my $i=$#$stuff; $i >= 0; --$i) { #use $i as index }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
subtle beauty.
by Anonymous Monk on Jan 28, 2004 at 19:32 UTC |