Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Lookup closest hash key

by The Perlman (Beadle)
on Jan 25, 2011 at 11:03 UTC ( #884099=note: print w/replies, xml ) Need Help??

in reply to Lookup closest hash key

my %distances = ( 452 => 'London', 678 => 'Paris', 890 => 'Rome', );
> would it be possible to get the value Paris by looking up the non existent key of 672?

Sure, add the non-existent key 672 pointing to Paris as nearest neighbor! :)

Seriously, a one kilometer grid of distances on earth only means precalculating at most 20000 entries (but better use an array then).

You can also go for a compromise and add a wider grid to the hash, like e.g. 10 kilometers. (The optimal width depends on your data).

Now rounding 672 to 670 and 680 will lead you to precalculated hash entries pointing to Paris. Can't be much faster...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://884099]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-01-17 09:36 GMT
Find Nodes?
    Voting Booth?