Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: line by line match on an array of strings

by jbert (Priest)
on Jan 09, 2008 at 10:42 UTC ( #661306=note: print w/replies, xml ) Need Help??

in reply to line by line match on an array of strings

You might get some minor joy by:
  • Compiling your regexps and saving them. See qr in perldoc perlop.
  • If each line can only match one of these typedefs, make sure you exit the typedef loop when you find a match.
  • In combination with the previous, if you have some typedefs which are more likely to occur, order the typedefs array with the most likely at the front (for early exit).
In combination:
my @typedefs = qw(...sorted list of typedefs, common ones first...); my @regexes = map { qr{$_} } @typedefs; REGEX: foreach my $regex (@regexes) { if ($line =~ $regex) { # found typedef so do work last REGEX; # Stop looking } }
Ah...if you want to perform the same action, you can munge all your typedefs together into one regex.
my @typedefs = qw(...sorted list of typedefs, common ones first...); # Make a big '(a|b|c|...)' regex str # Should probably do some quoting here. my $regexStr = "(" . join("|", @typedefs) . ")"; my $regex = qr{$regexStr}; if ($line =~ $regex) { # found typedef so do work }
That should help a bit, since you're now handling all the alternatives in the regex engine, i.e. looping in C not perl.

UPDATE: in case it's not obvious, you want to produce the @regex array or $regex once, before looping through all your lines. Don't do it per line :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://661306]
LanX gets eaten by a black hole popping up in the vacuum of his head
[Eily]: that a black hole formed there would rather be the sign of a head too full
LanX can't hint anymore to zentaras video because he disappeared

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-06-23 16:28 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (552 votes). Check out past polls.