This will work fine if the input data can be completely relied on to be in exactly the form you expect.

Two things can take a chunk out of your posterior: (a) the data is variable (in any way) -- anything generated by humans is suspect (of course), anything generated by systems written by humans may not be perfect (let's face it); (b) you didn't fully understand the problem. More insidious yet, nobody may notice that the output of this program is incorrect until something REALLY BAD happens.

So, I'd do something like:

if (/^LOGGING: \S+?-(\S+) \S+: (\S+) [^:]*: (\S+)$/) { my ($month, $what, $user) = ($1, $2, $3) ; if ($what eq 'STARTING') { $flushes{$month}{$user}++ ; $flushes{$month}{TOTAL}++ ; } ; } else { # Any error reporting you like, eg: chomp ; print STDERR "Line $.: '$_' ??\n" ; } ;
This is not a lot of extra code. Of course, you could do much more validation of the input and/or be less strict about some things (eg white-space and case)

Who knows, you might be relieved when you discover the file contains:

.... LOGGING: 31-Mar-2008 23:59:10: ENDING ON A HIGH: countryjoe LOGGING: 1-Apr-2008 00:01:22: STARTING FLUSH: gotcha ....
before the auditors.


In reply to Re^2: Extracting and counting string occurences by gone2015
in thread Extracting and counting string occurences by joec_

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.