I'd recommend two minor changes --

  1. Put the case insensitive flag at the top, rather than for each iteration of the loop. (I haven't benchmarked it, but I'm just one to do as much repetitive stuff before the loop, if at all possible)
  2. Use a single regex for matching, as you don't seem to be doing anything different based on which term within the loop matched.*

* I know, that's not specifically true, as with your case, you can match each item in the list once, but I'm making a general assumption that your items look to be SQL statements, and if they're single statements, they're most likely mutually exclusive, so long as you don't have sub queries

I'd probably rewrite it something like:

my $pattern = qr/\b(?:create|drop|delete|update|insert)\b/i; # 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); if ($date->[13] =~ $pattern) { print "$data->[3] $data->[9] $data->[10] $data->[13]\n"; } }

You can also use perl's foreach loops to deal with iterating through a list, when the actual index isn't important. (yes, I know, it can be called with 'for', but I always think of C's for loops when I use 'for')


In reply to Re: speeding up a regex by jhourcle
in thread 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.