[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Closest hash value



> Depending on how densely populated your hash is you might want to use.
> 
> $key = 5;
> $i=0;
> do
> {
> 	$match = $h{$key + $i} || $h{$key - $i};
> 	$i++;
> } until defined $match;
> 
> I think this is a gain on something which iterates over all hash keys if
> the average distance between keys is less than the total number of keys in
> the hash. That said it could be a very big loss on a sparse hash like this
> 
> (
> 	0 => 'dog',
> 	10000 => 'cat',
> 	100000 => 'cow',
> )
> 
> 
> Fergal

Actually, thats a pretty good solution for the data, as the keys tend to 
be pretty clumpy. As you point out, not desireable for the general case.
With some modification, this approach could find the upper and lower key
and do linear interpolation.

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe