in reply to mapping lists

Another binary chop solution

#! perl -slw use vars qw[$MAX $SIZE $LO $HI]; use strict; $MAX ||= 100; $SIZE ||= 10; $LO ||= 25; $HI ||= 50; sub bsearch { my ($ref, $val) = @_; my ($lo, $hi) = (0, $#$ref); while ($lo < $hi) { my $i = ($lo+$hi)>>1; my $bool = $val <=> $ref->[$i]; if ($bool < 0) { $hi = $i; } elsif ($bool > 0) { $lo = $i + 1; } else { return $i; } } return $hi; } sub range { my ($ref, $lo, $hi) = @_; my $lidx = bsearch($ref, $lo); $lo = ($ref->[$lidx] > $lo and $lidx > 0) ? $ref->[--$lidx] : $ +ref->[$lidx]; my $hidx = bsearch($ref, $hi); $hi = ($ref->[$hidx] < $hi and $hi < $#$ref) ? $ref->[$hidx++] : $ +ref->[$hidx]; $lo .. $hi; } my @F; my ($start, $end) = (0,0); #! Gen some test data while ($start + $SIZE < $MAX) { $start += int rand($SIZE); $end = int $start + rand($SIZE); push @F, $start .. $end; $start = $end; } #! display it print "@F"; #! and a range from it encompassing two (possibly absent values) print "@{[ range(\@F, $LO, $HI) ]}"; __DATA__ C:\test>229675 -MAX=50 -SIZE=5 -LO=15 -HI=35 2 3 4 5 5 6 7 8 9 12 13 17 18 19 20 21 21 22 23 24 25 29 30 31 33 34 3 +6 37 40 41 42 43 44 45 46 47 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 3 +6

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.