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.


In reply to Re: mapping lists by BrowserUk
in thread mapping lists by belg4mit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.