Those are for building binary trees, which wouldn't really suit your purpose. There are amost certainly one (or more) modules on CPAN that would provide for binary searching a sorted array, but the usual pattern for these is that they return undef or -1 to indicate that the target value wasn't found, whereas you want to retrieve the index of the nearest lower value so that you can then perform your differencing between the target value and the two nearest. Ie. The values at the index returned and that index +1.

Here's some code that will do that. Note I didn't need to sort my test array as it is already sorted.

#! perl -slw use strict; sub bsearch { my( $aref, $value ) = @_; my( $lo, $hi, $mid ) = ( 0, $#{ $aref } ); while( $lo <= $hi ) { $mid = int( ($lo + $hi) / 2 ); if( $value > $aref->[ $mid ] ) { $lo = $mid + 1; } elsif( $value < $aref->[ $mid ] ) { $hi = $mid - 1; } else { #Found an exact match so return that index return $mid; } } # Didn't find an exact match so return the index below where we st +opped # Or if the value is lower than the # lowest element, return 0 return $aref->[ $mid ] > $value ? $mid - 1 : $mid; } my @array = 0 .. 100; my $iNearest = bsearch \@array, 31.5; print 31.5, ' : ', $iNearest, ' : ',$array[ $iNearest ]; print "$_ => @{[ bsearch \@array, $_ ]}" for -1, 0, 1, 2, 31.1, 31.999, 50, 98, 99, 100, 101; __END__ P:\test>bsearch 31.5 : 31 : 31 -1 => 0 0 => 0 1 => 1 2 => 2 31.1 => 31 31.999 => 31 50 => 50 98 => 98 99 => 99 100 => 100 101 => 100

You could also code this using a recursive algorithm, but I tend to avoid recursion if the iterative solution doesn't require me to maintain my own stack.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: Re: Re: Re: Re: Find a number in a list closest to a target by BrowserUk
in thread Find a number in a list closest to a target by gnu@perl

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.