in reply to mapping lists
Note: my version returns only elements actually in the list. If you want all the integers in between, just return $F[$lo_pos]..$F[$hi_pos].
Update: It now appears that the index positions are wanted instead. No problem, that's just $lo_pos..$hi_pos.
use strict; use Search::Binary; my $lastIndex; sub reader { my ($handle, $val, $index) = @_; if (!defined $index) { $lastIndex++; } else { $lastIndex = $index; } return ($val <=> $handle->[$lastIndex], $lastIndex); } my @F = ( 1, 2, 4, 8, 16, 32..64 ); sub range{ my($lo, $hi) = @_; my $lo_pos = binary_search(0,$#F,$lo,\&reader,\@F); my $hi_pos = binary_search(0,$#F,$hi,\&reader,\@F); $hi_pos = $#F if ($hi_pos > $#F); return @F[$lo_pos..$hi_pos]; } print join(',', range(25,35)), "\n"; print join(',', range(50,75)), "\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: mapping lists
by tall_man (Parson) on Jan 24, 2003 at 22:15 UTC |