Help language development. Donate to The Perl Foundation
A simple module to find the nearest element in a ordered list without processing all elements.
The grep sub will process all elements in a list, which is slow when you have an ordered list of numbers.
Hashes only work for keys matching exaclty.
use Numeric::Nearest; my @l=1,2,3,4; say nearestPair(1.2, @l);
The closest entry in the list to 1.2 is 1.
Returns a pair of index => value of the element of $list which is nearest in magnitude to $key
Returns a sequence of index => values, one for each element in the $keys variable.