in reply to Matching against list of patterns
The typical method is to dynamically build an alternation RE and let the RE engine do all the optimisation and hard work in lovingly hand optimised C. We use qr// so the RE is compiled once and avoid vast numbers of hash lookups, functionally useless variable assignments*, and RE compiles that you are doing above. As a general rule loops are great territory for optimisation due to the repeated execution of the loop code.
my @terms = qw( foo bar baz fool foolish ); my $re = join '|', map{quotemeta} sort {length($b)<=>length($a)}@terms +; $re = qr/($re)/; while(<DATA>){ my @matches = $_ =~ m/$re/g; next unless @matches; print "Got @matches\n" } __DATA__ Only a foobar foolish fool skips the sort by length Without this foo you look foolish and may match short elements!
Hash lookup tables may still have a place to convert a matched value into something else. Here is a trivial example. Beware /i as you need to lc($1) and use all lowercase keys in %terms or you won't get a match/lookup.
my %terms = ( foo => 'FOO', bar => 'BAR' ); my $re = join '|', map{quotemeta} sort {length($b)<=>length($a)}keys % +terms; $re = qr/($re)/i; while(<DATA>){ s/$re/$terms{lc($1)}/g; print; } __DATA__ foo is a word Bar ba blacksheep
cheers
tachyon
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Matching against list of patterns
by Random_Walk (Prior) on Sep 16, 2004 at 14:26 UTC | |
by Eyck (Priest) on Sep 17, 2004 at 07:09 UTC | |
by Random_Walk (Prior) on Sep 17, 2004 at 08:40 UTC | |
by Eyck (Priest) on Sep 17, 2004 at 09:40 UTC | |
by Random_Walk (Prior) on Sep 17, 2004 at 12:00 UTC | |
Re^2: Matching against list of patterns
by Eyck (Priest) on Sep 16, 2004 at 13:42 UTC | |
by tachyon (Chancellor) on Sep 16, 2004 at 23:00 UTC | |
by Eyck (Priest) on Sep 17, 2004 at 09:50 UTC |
In Section
Seekers of Perl Wisdom