I'm writing a module to (I hope) efficiently parse large logfiles with a variable entry length (most of the log entries are more than one line, some several hundred lines long). With each log entry I also need to extract the time/date.

As I go about it right now I have a regular expression matching the beginning of each log entry, with parenthisized subexpressions capturing year, day, month, hour, minute and second of the entry.

I load a chunk of the logfile in question into $_, using sysread. Then I split this into log entries, and subexpressions and put it into an array local to my module. The array looks like this:

[ [ "LOGENTRY 1", $1, $2, $3, $4..], [ "LOGENTRY 2", $1, $2, $3, $4..], ... ]

Each subsequent call to my module's next_entry() simply shifts the top element off the array and return that, until there's only one element left -- then I add another chunk from the logfile, splits that into an array-of-arrays and start the whole circus over again.

However, I'm having some problems in the logfile chunk parsing. Right now, it looks like this:

while (/$regex/g) { push @buffer, [ substr($_, $last, $-[0]-$last), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, ]; $last = $-[0]; }

At first I tried

while (@x = /($regex)/g) { push @buffer, [ @x ]; }

..but to my suprise that didn't work. Turns out that the m//g in list context returns one list of all subexpressions matched, rather than (as m// without the /g flag) one list of subexpressions per match.

How should I go about this? Perhaps there's some better method that I have completely missed?

P.S. Note that my regex does not match an *entire* log entry, just the beginning of it.


In reply to Efficient log parsing? by zrajm

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.