Okay. If both arrays are sorted, you don't need a binary search.

You just move through them in parallel and you're done in a single pass:

#! perl -slw use strict; my @matches = map $_ * 10, 1 .. 10; my @headers = map $_ * 5 +2, 0 .. 20; my $hdr = 0; for my $match ( @matches ) { $hdr++ while $headers[ $hdr + 1 ] < $match; printf "match at %d in section from %d - %d\n", $match, $headers[ $hdr ], $headers[ $hdr + 1 ] -1; } __END__ [16:20:40.99] c:\test>junk36 match at 10 in section from 7 - 11 match at 20 in section from 17 - 21 match at 30 in section from 27 - 31 match at 40 in section from 37 - 41 match at 50 in section from 47 - 51 match at 60 in section from 57 - 61 match at 70 in section from 67 - 71 match at 80 in section from 77 - 81 match at 90 in section from 87 - 91 match at 100 in section from 97 - 101

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^4: search array for closest lower and higher number from another array by BrowserUk
in thread search array for closest lower and higher number from another array by bigbot

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.