Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Lookup closest hash key

by The Perlman (Scribe)
on Jan 25, 2011 at 11:03 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-03-29 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found