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
In reply to Re: processing a list of events
by GotToBTru
in thread processing a list of events
by BluePerlDev
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |