Go back to the script that generates the log in the first place and store the epoch instead of the data you have. (or in addition to this stuff if you like to read it)
Then you can just subtract one epoch from the other to get total number of seconds. (The epoch is returned from time() )

So, for example, if your log now reads:

964644652 Wed Jul 26 13:50:52 2000 964644691 Wed Jul 26 13:51:31 2000
You can parse this like so:
use strict; # Always. my $logname = "log.txt"; open LOG, $logname or die "Failed to open $logname, $!"; my( $start, $stop ); while( $start = <LOG> ) { ($start) = split /\s/, $start; $stop = <LOG> or die "No End Time in LOG!\n"; ($stop) = split /\s/, $stop; my $time_in_seconds = $stop - $start; # Do something with $time_in_seconds print "$time_in_seconds\n"; } close LOG or die "Failed to close $logname, $!";
To get the log format I gave you I used:
use strict; my $t = time(); print $t, "\t", scalar localtime($t)";

Update: Tye Pointed out that split will take care of the remainder for me, so I took that out.

Also, you might want to note in the log which lines are 'start' and which are 'stop' so that you can catch errors (like failing to record the 'stop' time). Right now, you would only notice such an error if there is an odd number of occurances. (And only at the end of the script when it can't find an end time for the last entry.) Of course, then you would need to parse that extra bit of info from the log. But entries like:

START 964644691 Wed Jul 26 13:51:31 2000 STOP 964644691 Wed Jul 26 13:51:31 2000
should not be hard to parse. just call split /\s/ as before, and make the recieving array ($tag, $epoch) or whatever.

In reply to (Adam: Use a better log format) RE: parsing by Adam
in thread parsing by toadi

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.