Tachyon,
I have a similar problem, over six hundred complex regexen to match against a busy logfile and related messages to issue depending on which regexps were matched. much the same problem as the OP
If I understand the regexp engine caching the compiled version of a regex if it is not going to change then I think this should be a reasonably efficient approach. Am I on the right tracks ? and is the /o unrequired as I have already interpolated the variable when the regex is first called ?
#!/usr/local/bin/perl -w
use strict;
my ($i, $compile_me, @names)=(1, "{my \@matches;", "no match");
while (<DATA>) {
next if /^\s*$/;
last if /END CONFIG/;
chomp;
my ($name, $reg)=split;
push @names, $name;
$compile_me.="push \@matches, $i if /$reg/o;";
$i++;
}
$compile_me.="\@matches}";
while (<DATA>) {
chomp;
print "\nmatches found for $_\n";
my @matches=eval $compile_me;
foreach (@matches) {
print $names[$_] , "\n"
}
}
__DATA__
Fred_and_Friends fr.d
Paul_and_co paul
some_numbers \d{2}
freud_likes_fred fr
END CONFIG
freud
fred
NaNa
pauline
12312sdfsdf
2
Update with speed test
I have now run a comparative test over 300^H^H^H, sorry 416 lines of log, with my 672 pattern matches. First using the eval of a string containing all the regexen and returning match index numbers as above. Second is my old naive code holding an array with the regexen and doing a foreach through it against each line. I did not use the /o for the reasons given above it works fine without it
>time ./Monitor.fast
real 0m1.49s
user 0m0.68s
sys 0m0.58s
>time ./Monitor
real 0m19.47s
user 0m14.69s
sys 0m0.50s
>
I think the numbers speak for themselves
Cheers,
R.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.