in reply to Re^3: search array for closest lower and higher number from another array
in thread search array for closest lower and higher number from another array
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: search array for closest lower and higher number from another array
by bigbot (Beadle) on Feb 05, 2011 at 16:46 UTC | |
|
Re^5: search array for closest lower and higher number from another array
by bigbot (Beadle) on Feb 07, 2011 at 06:57 UTC | |
by BrowserUk (Patriarch) on Feb 07, 2011 at 09:50 UTC | |
by bigbot (Beadle) on Feb 07, 2011 at 11:39 UTC | |
by bigbot (Beadle) on Feb 07, 2011 at 11:02 UTC | |
by BrowserUk (Patriarch) on Feb 07, 2011 at 11:40 UTC | |
by bigbot (Beadle) on Feb 08, 2011 at 01:27 UTC |