On May 5, Jim Conte said:

>the ($key, $value) of %one is a span of time.
>
>I need to compare this span of time over every $key of %two.
>
>while (($key1, $val1) = each (%one)) {
>
>       while (($key2, $val2) = each (%two)) {
>
>               if (($key2 >= $key1) && ($key2 < $val1)) {
>                       
>                       push @match, $val2;
>               }
>       }
>}

I would change your looping to go over %two first.  I would also keep the
ranges in sorted order in an array.

  # sort ranges by their starting time
  my @ranges =
    map { [ $_, $one{$_} ] }
    sort { $a <=> $b }
    keys %one;

  while (my ($time) = each %two) {
    next if $match{$time};  # in case of duplicates

    for (@ranges) {
      # last if the time is ok for the range
      last if $match{$time} = ($_->[0] <= $time && $time < $_->[1]);

      # last if the time is too small for the rest of the ranges
      last if $time < $_->[0];
    }
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734

Reply via email to