Here's a quick example showing your problem can be done using a binary search, even though the "25" is not in the list.
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";
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.