Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Hash tables, are they really what we see?

by dsheroh (Prior)
on Oct 03, 2012 at 08:26 UTC ( #997006=note: print w/replies, xml ) Need Help??

in reply to Hash tables, are they really what we see?

But let`s assume we have a hash with, for example over 1 000 000 entries as a word count and we then search for a word that just does not appear there? How in blazes Perl will know that there is not that word from a million of words!? I just picture a barrel filled with red and blue balls over a million and you have to tell that you are 100% there is no other colors by just looking at that barrel.
Your barrel is the wrong image for visualizing a hash. The magic of hashes is that you don't do hash searches, you do hash lookups. When you ask for $hash{foo}, Perl doesn't have to examine every key in %hash to see whether foo is among them, instead, it calculates "If a key foo exists, then it will be in this location.", then looks only in that location.

For a better real-world image, think of a hotel with mailboxes on the wall behind the front desk. When you want to see whether you have any mail, you don't tell the clerk your name and then wait for them to check every piece of mail to see who it's addressed to. Instead you say "I'm in room 234" and the clerk looks only at the box numbered 234. If there's anything in that box, you have mail; if there isn't, you don't. (In this example, you stating your room number is analogous to the hashing function used by Perl to map hash keys to "buckets" in the hash.)

Replies are listed 'Best First'.
Re^2: Hash tables, are they really what we see?
by heatblazer (Scribe) on Oct 03, 2012 at 08:56 UTC

    In abstraction you mean let`s say it prereserves a cells in memory that are expected to be either full or empty so when I tell him, "perl I want a key name1, which I don`t remember if I sotred.", he looks into some 0xFF00BB3012, which expects to be in the memory for the hash and tells me: "nope, I don`t have what you want in that address location so I won`t give you the coresponding value from the coresponding address." Quite abstract stuff, but as far as I got it, perl kind of flags these addresses and then directly access them with lowlevel operations which are too much for me to bear...

Re^2: Hash tables, are they really what we see?
by heatblazer (Scribe) on Oct 03, 2012 at 09:09 UTC

    Also your answer, enlightened me why hashes are unsorted. They are actually sorted by memory addresses for perl, and we see them as unsorted the way we expected them.

    ... if my logic is true.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997006]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-06-23 06:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.