dru145 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

Does anybody have any suggestions on the best way to learn hashes? So far this has been the most difficult aspect of learning Perl for me. I'm going to be a Friar soon and I think it's sorry that I don't even know how to use hashes yet. I know your level has nothing to do with how well verse you are in Perl, but I don't think it's too cool.

Anyway, can any other monk relate to my situation? What did you do to overcome this hurdle? Was there a particular section in a book or website that helped?

Just for the record, here is what I've used so far to try and grasp them:
  1. Learning Perl
  2. Elements of Programming Perl
  3. Perl Little Black Book
  4. Q&A Hashes
I've got the basics of keys and values down, but I'm lost when actually have to use them in my code.

Thanks,
Dru

Replies are listed 'Best First'.
Re: Trouble with Hashes
by chromatic (Archbishop) on Dec 19, 2001 at 11:19 UTC
    If you understand arrays, hashes aren't really that different. Suppose you have an array called @race_results, containing an ordered list of the names of people who finished a race. Ignoring the 0th element to make things convenient, you could associate the position in the array with the name as follows:
    1 -> Bob 2 -> Joe 3 -> Sunny 4 -> Hannah
    It follows, if you want to find out who came in first, you would use the code $race_results[1].

    If you follow that, then hashes aren't too difficult. The only effective difference is that they're not accessed by number. There's no particular order. Instead, it's like a dictionary, where you look up a definition by the word. You might create a hash called %race_results like this:

    my %race_results = ( first => 'Bob' second => 'Joe', third => 'Sunny', fourth => 'Hannah', );
    To find out who came in first, say $race_results{first};.

    Think of it like an array, where you use strings instead of numbers, and you'll have it. (It's not completely accurate under the hood, but that's how I first approached it, and it's a decent mental model.)

    Use them whenever you have one string to associate with another, and you want to be able to look something up as you would in an encyclopedia or a dictionary. If you can look at the world in terms of indexes, you'll have your keys.

Re (tilly) 1: Trouble with Hashes
by tilly (Archbishop) on Dec 19, 2001 at 11:43 UTC
    The best single tip on using hashes for me was a bit in Programming Perl (2nd ed, it is in the 3rd as well) about how to translate Perl data structures into English.

    A hash lookup is the word "of".

    So, for instance, if you want to be able to find the address of a person, you would name the hash %address, make the keys be the $name of the person, and addess the address with:

    print "$name has an address of $address{$name}\n";
    (Read the hash lookup as, "address of $name".)

    I find this little tip to be very useful in figuring out where you can use hashes, what to call them, and how to read code written with them. After that take a look in the Cookbook for some of the hash tricks, and as you read them see how they fit in this mental model. The answer generally is, "Pretty well."

Re: Trouble with Hashes
by joealba (Hermit) on Dec 19, 2001 at 19:11 UTC
    I've had this page bookmarked for a while just in case I ever get stuck with a nasty hash.

    This site - http://www.rocketaware.com/perl/ - has a pretty complete and organized, yet ugly interface to many Perl resources, FAQ's, perldoc files, and some tutorials.