Hi, I have a couple of questions surrounding the speeding up of a regex I've written. Please bear in mind I'm not a Perl guru so I'd like to learn a bit from the masters on how best to write more efficent code.

Here's a snippet of my code

my @patterns = ( qr/\bcreate\b/, qr/\bdrop\b/, qr/\bdelete\b/, qr/\bupdate\b/, qr/\binsert\b/, ); open a database connection here (using Sybase::DBD) and create a statement string to execute $sth=$dbh->prepare("@sqlstatement"); $sth->execute; while ($data = $sth->fetchrow_arrayref()) { next if($data->[10] =~ /tempdb/i); for ($loop_index = 0; $loop_index < $#patterns; $loop_index++) { if($data->[13] =~ /$patterns[$loop_index]/i) { print "$data->[3] + $data->[9] $data->[10] $data->[13]\n"; } } }
This extracts an audit trail from a sybase database and examines each row via the regex above. It produces the output I want but I can't help feeling there's a more efficient way of doing the regex bit.

The first question is , if I wanted to get the keywords from a file (so I can build up a dictionary of keywords to search for) instead of hardcoding them as complied regular expressions as in the code above , how would I do this ? e.g. assume my keyword input file would look like this

create delete insert update drop

Is there a more efficient way of doing the regex ?... I read somewhere about the possibility of using the study function to improve the performance.

Any help appreciated, thanks in advance


In reply to speeding up a regex by Anonymous Monk

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.