Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Simple log parser

by dimmesdale (Friar)
on Aug 15, 2002 at 14:20 UTC ( [id://190407] : note . print w/replies, xml ) Need Help??

in reply to Simple log parser

@lines = <LOGFILE>; foreach $line (@lines) { if ($line != m/\s/) { # regex to turn dates into epoch ($dd, $mm, $yy, $hh, $mt, $tt, $tdd, $tmm, $tyy, $thh, $tmt, $ttt) = $ +line =~ /(\d+)-(\d+)-(\d+)\s(\d+):(\d+)\s(am|pm)-(\d+)-(\d+)-(\d+)\s( +\d+):(\d+)\s(am|pm)/;
You say dd-mm... but then in the regex you have \d+. If you want to have just two digits, then something like this would work:
$line =~ /(\d\d)-(\d\d)\s+(\d\d):(\d\d)\s+([ap]m)-(\d\d)-(\d\d)-(\d\d) +\s+(\d\d):(\d\d)\s+[ap]m/

Notice I changes (am|pm) to [ap]m. Character class is faster than alternation, and since m is repeating in both you don't need to have that included in the alternative. Have you looked at the various Date:: modules?

if ($tt == 'pm') { $hh = $hh + 12; } if ($ttt == 'pm') { $thh = $thh + 12; }

Uh-ohh.... someone didn't have use strict/use warnings on ... did they?

if ($tt eq 'pm') { $hh += 12; } if ($ttt eq 'pm') { $thh += 12; }

You'll notice I used the perlop 'eq' here (== is numeric, we don't want numeric; use strict/use warnings issues a warning for this I believe). I also changed the $a = $a + $b form to $a += $b. Not necessary, but it looks a little better (to me :).