Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Efficiency: Foreach loop and multiple regexs

by jkahn (Friar)
on Sep 13, 2002 at 15:50 UTC ( #197623=note: print w/ replies, xml ) Need Help??


in reply to Efficiency: Foreach loop and multiple regexs

neilwatson said:

foreach $x (@regex){ @bad_matches = grep ! m/$x/, @array; }
After the grep how would I remove the entries in @bad_matches from @array before the next $x? Would this be faster?

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.


Comment on Re: Efficiency: Foreach loop and multiple regexs
Select or Download Code
Re: Re: Efficiency: Foreach loop and multiple regexs
by waswas-fng (Curate) on Sep 13, 2002 at 16:42 UTC
    I like this, it is pretty fast and yet readable:
    #!/usr/local/bin/perl my @regexs = ('test12','test2','test3','test4'); my $filename = '/var/tmp/testdata'; my @compregex = (); my (%match, %nomatch) = (); # Pre-compile regexes, sorting smaller to larger # (shorter prolly will match more than a longer =) foreach my $regex (sort { length($a) <=> length($b) } @regexs) { print "Building regex for $regex\n"; push @compregex, qr/$regex/; } open FILENAME, $filename; # could rewrite this with map, used while and foreach cause # most people know how they work, some dont understand map =) while (<FILENAME>) { chomp; foreach my $test (@compregex) { ($match{"$_"})++ if (/$test/) ; last if $match{"$_"}; #found a match no need t +o go further } ($nomatch{"$_"})++ unless $match{"$_"}; } print "Matches:\n"; foreach my $X (keys %match) { print "$X\n"; } print "no Matches:\n"; foreach my $X (keys %nomatch) { print "$X\n"; }


    -Waswas-fng

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://197623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-07-02 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (47 votes), past polls