Make sure you put the most common codes at the front of your @codes array for more speed, since you'll do less searching.

Interesting idea... I wonder if dynamically resorting as you go would help?

my @codes = qw/ CODE1 CODE2 CODE3 /; my %hitCounts; my @regex = map { $hitCounts{$_}=1; qr/$_/i } @codes; # tune this parameter for optimal performance, balancing better orderi +ng # of regexen with sort costs... my $resortFreq=1000; my $iterCount=0; while (my $inputline = <FILE>) { my $found = 0; foreach my $regex(@regex) { if ($inputline =~ /$regex/) { $hitCounts{$regex}++; $found = 1; last; } } # re-sort every 1000 lines. The "1000" is a parameter that prolly + should # be tuned if(++$iterCount%$resortFreq == 0) { @regex=sort {$hitCounts{$b}<=>$hitCounts{$a}} @regex; } next unless $found; # logic goes here }
At the very least, you could do this once, and then feed the results back into the top of the script. If the frequencies are about constant (which makes sense for department populations in a large firm, I guess), this should make a lot more sense than dynamically resorting each time...
END { print "Regexen in sorted order:\n\t"; print join "\n\t",sort {$hitCounts{$b}<=>$hitCounts{$a}} @regex; print "\n"; }
An other alternative would be to have the end block write out a file of regexen, which the script could read back in. Then each run would be as good as it could be, based on the results of the previous run...
--
Mike

In reply to Re: Re: Bulk Regex? by RMGir
in thread Bulk Regex? by meetraz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.