in reply to Efficiency: Foreach loop and multiple regexs
After the grep how would I remove the entries in @bad_matches from @array before the next $x? Would this be faster?foreach $x (@regex){ @bad_matches = grep ! m/$x/, @array; }
I would suggest that you use the following mechanism (incorporating some ideas from other contributors):
foreach my $regex (@regexes) { @array = grep { ! m/$regex/ } @array; }
If you really need to keep @bad_matches (those that *did* match the regexes) then you probably want to run the loop manually, as somebody else suggested:
my (@array); # is populated however you originally did my (@keepers); my (@bad); ITEM: # label added while (@array) { my $item = shift @array; foreach my $regex (@regexes) { if ($item =~ $regex) { push @bad, $item; next ITEM; # return to label } } push @keepers, $item; } # if you want it back on @array, just uncomment: # @array = @keepers;
HTH, -- jkahn
Update:(09:37 PDT) added ITEM: label and next ITEM line. Whoops. Code will be substantially sub-optimal without them.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Efficiency: Foreach loop and multiple regexs
by waswas-fng (Curate) on Sep 13, 2002 at 16:42 UTC |