Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: why use a hash instead of an array

by davido (Archbishop)
on Jun 11, 2013 at 18:57 UTC ( #1038325=note: print w/replies, xml ) Need Help??

in reply to why use a hash instead of an array

Hashes provide constant time lookups by key as opposed to constant time lookups by index. Imagine if your dictionary were 0: Apple, 1: Ball, 2: Cat, 3: Dog. Find the word at index #2. That's done in constant time. Now find for me Cat. You have to either do a linear search, or a binary search to find it (linear time, or log2(n) time. You would never remember that 4200 is Quagmire, so you end up doing time-consuming searches a lot.

Now let's organize your dictionary like this: "Apple: A fruit. Ball: A toy. Cat: A skittish animal. Dog: A loyal animal. ..... Quagmire: A difficult situation". Quick, find if Quagmire is in the dictionary. Wow, we just did it in constant time (no searching), using a hash.

That turns out to be really, REALLY useful. We're not always just looking up words in dictionaries. We're referring to attributes in objects, we're manipulating sets, we're memoizing and caching, and we're even implementing variable systems (Perl's package globals reside in a glorified hash, internally).

In computer science one would look at hashes as yet another datastructure. And for many problems, selecting the right data structure is 9/10ths of the solution. As you get into programming, I recommend getting a good introductory book on data structures and algorithms. I wish I had a suggestion for you. Mastering Algorithms with Perl is probably a little advanced at this point, and it's getting a little old too. But start looking at, well, what is an array, and what are its characteristics? What is a linked list, and what are its characteristics? Binary Tree, Hash, and so on.


Replies are listed 'Best First'.
Re^2: why use a hash instead of an array
by AnomalousMonk (Canon) on Jun 12, 2013 at 01:55 UTC

    While it is aimed, I think, more at the intermediate programmer, chromatic's freely downloadable Modern Perl might be a good beginners intro to Perl. And while I can't recommend Wikipedia as a beginners resource because it gets rather CS-ish rather quickly, all the topics davido mentioned and more are there and may serve as useful points of departure to more accessible on-line resources: array; linked list; binary tree; hash, or associative array; ...

    And you are always welcome at The Monastery Gates.

Re^2: why use a hash instead of an array
by DarrenSol (Acolyte) on Jun 13, 2013 at 15:01 UTC
    jeez. i did a web search and went through a number of the pages that came up, but didn't find a reason why to use a hash that's as quick and clear as your quagmire example. thanks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1038325]
[stevieb]: man, I absolutely LOVE driving in the worst winter conditions. In the mountainous roads, there are extremely few (if any) other drivers. I love the challenge. I've been doing extreme off-road my whole life, and as I said, I'm always well...
[stevieb]: ...prepared for even the very worst
Discipulus seems a part of the globe very unfrindly with Kawasaki riders..
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2016-12-06 16:50 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.