in reply to Re: Efficiency: Foreach loop and multiple regexs
in thread Efficiency: Foreach loop and multiple regexs
#!/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"; }
|
|---|