Okay, I'll give this a shot; it appears you're looking for the logic, and not the code (which is good, because I'm not that good at Perl yet :). Anyway, here's my attempt at pseudocoding your problem:

my ($starttime, $isopen, $totaltime) = (0, 0, 0); while (<TIMELOG>) { if (/open$/) { next if $isopen; $isopen = 1; $starttime = getseconds($_); } elsif (/hold$/ || /closed$/) { next unless $isopen; $isopen = 0; $totaltime += getseconds($_) - $starttime; } }

Well, that came out a bit closer to code than I had intended... oh well. I hope that helps.

Update: (1000 CDT 09 Jun) It occurred to me a bit more explanation might be in order. Basically, as I understand it, the key to your problem is to keep track of the first time you noticed the ID was in an open state, and only perform the addition to $totaltime when the ID moves out of the open state (and presumably, into one of the closed or hold states).

Also, having watched a bit of the CB discussion on this topic, I noticed that you want to run this in realtime. If that's the case, you may want to generate a fake log entry to be processed at the end with the current time and a non-open state to force the calculation of the open time. This would change the code to be more like this:

@lines = <TIMELOG>; push @lines, makefake(time(), "closed"); for (@lines) { # Same loop body }

Update #2: Edited code and above comments on real time about current state.

I know it might not be the optimal method to do it, but it seems as if it should get the job done (if I understand the job to be done correctly). Try it out - if it doesn't work, let me know. If it does, let me know then, too - I never know when I might need this bit of code. :)

CheeseLord


In reply to Re: Re: Re: Funky date question. by CheeseLord
in thread Funky date question. by Mark 1

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.