I suggest you first parse your timelog and put the data into a data structure that you can then more easily traverse.
use Modern::Perl qw/2014/; use DateTime; use DateTime::Format::Flexible; use DateTime::Duration; my %data; while (<DATA>) { chomp; my ( $datetime, $msecs, $id, $description ) = split /,/; $msecs *=1000000; $datetime .= ":$msecs"; my $dt = DateTime::Format::Flexible->parse_datetime($datetime); $data{$id}->{$description} = $dt; } for my $id ( keys %data ) { print "Results for $id: "; if ( exists $data{$id}{'P5 Move request'} and exists $data{$id}{'TP Service Request'} ) { my $duration = $data{$id}{'P5 Move request'} ->subtract_datetime_absolute( $data{$id}{'TP Service Request +'} ); say "$data{$id}{'P5 Move request'} -> $data{$id}{'TP Service Request'} = " +, $duration->seconds + $duration->nanoseconds/1000000000, ' se +conds'; } else { say 'Missing data'; } } __DATA__ 2014-05-29 10:22:21,880,165ab6a8-e736-11e3-8748-8d365226be24,TP Servic +e Request 2014-05-29 10:22:21,962,165ab6a8-e736-11e3-8748-8d365226be24,ProcessNa +me: TC 2014-05-29 10:22:21,965,165ab6a8-e736-11e3-8748-8d365226be24,P5 Starte +d (...) 2014-05-29 07:14:04,048,1770ebca-e722-11e3-b793-c6903cc19f13,P5 Move r +equest
Output:
Results for 1770ebca-e722-11e3-b793-c6903cc19f13: Missing data Results for 165ab6a8-e736-11e3-8748-8d365226be24: 2014-05-29T10:23:08 +-> 2014-05-29T10:22:21 = 46.426 seconds
As you see, calculating the time between a "P5 Move request" and "TP Service Request" gets really easy: a few look-ups in the data structure is all that is needed. As the datetime is a DateTime-object, calculating the difference (i.e. duration) between two datetimes requires no complicated calculations.

Two more comments:

  1. If for the same ID you can have multiple processes with the same name, this data-structure is too simple as a later process will overwrite the data of a previous one.
  2. Always use a DateTime (or similar) object to represent a datetime. Time and date calculations are fraught with exceptions and special rules. Not all hours are 60 minutes and not all minutes are 60 seconds. The DateTime module takes care of all of that.

Update: Amended code to take into account the millisecond field in the timelog.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

In reply to Re: processing timelog by CountZero
in thread processing timelog by sandy105

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.