Hi Bryan - Well, it ain't pretty, but this seems to work:
use strict; use warnings; my @starts = (3, 4, 15); my @stops = (5, 10, 20); my %ranges; PAIR: for (my $i = 0; $i <= $#starts; ++$i) { for (keys %ranges) { if ($starts[$i] > $_ and $starts[$i] < $ranges{$_} and $stops[$i] > $ranges{$_}) { $ranges{$_} = $stops[$i]; next PAIR; } } $ranges{$starts[$i]} = $stops[$i]; } print "$_ => $ranges{$_}\n" for (sort { $a <=> $b } keys %ranges); my $elapsed = 0; $elapsed += $ranges{$_} - $_ for (keys %ranges); print "elapsed = $elapsed\n"; it outputs: 3 => 10 15 => 20 elapsed = 12 As you can see, I load the start/stop pairs into a hash (%ranges) with the key the start time, and the value the stop time. Aloha => Beau. PS: Anyone have a solution with some elegance? -----Original Message----- From: Bryan R Harris [mailto:[EMAIL PROTECTED]] Sent: Wednesday, May 29, 2002 1:58 PM To: [EMAIL PROTECTED] Subject: union of times algorithm I'm trying to come up with an algorithm that seems like it ought to be really easy, but it's turning out to be pretty tough... Basically I have three pairs of start/stop times, e.g.: 3, 5 4, 10 15, 20 I want the total time covered by all these ranges. I can't just say (5-3 + 10-4 + 20-15) because the 3,5 and 4,10 ranges overlap. The desired answer for this case is 13, 3 to 10 and 15 to 20. I have the start times in two arrays, @starts and @stops. I have to do this probably a million+ times. Any ideas on how I could go about this? TIA. - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]