use strict; use warnings; use List::Util qw/first/; my (@list); push @list, {type=>'A',time=>1000}; push @list, {type=>'A',time=>1002}; push @list, {type=>'B',time=>1004}; push @list, {type=>'B',time=>1006}; push @list, {type=>'A',time=>1008}; push @list, {type=>'B',time=>1009}; my $ea = first { $_->{type} eq 'A' } @list; my $eb = first { $_->{type} eq 'B' && $_->{time} >= $ea->{time} } @lis +t; printf "A %d B %d Diff %d\n", $ea->{time},$eb->{time},$eb->{time}-$ea- +>{time};

Update: all pairs

my ($i,$j); while (1) { $i = -1; $j = -1; my $ea = first { $i++; $_->{type} eq 'A' } @list; last unless $ea; my $eb = first { $j++; $_->{type} eq 'B' && $_->{time} >= $ea->{time +} } @list; last unless $eb; printf "A %d B %d Diff %d\n", $ea->{time},$eb->{time},$eb->{time}-$e +a->{time}; splice @list,$j,1; splice @list,$i,1; }
A 1000 B 1004 Diff 4 A 1002 B 1006 Diff 4 A 1008 B 1009 Diff 1
Dum Spiro Spero

In reply to Re: processing a list of events by GotToBTru
in thread processing a list of events by BluePerlDev

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.