Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Little tricks

How do I create and use a hash of hashes? by MikeLacey
Posted: 4 Mar 02 (Edited 5 Oct 04)

Please browse through FAQ219-2884 and FAQ219-2889 first. Comments on this FAQ and the General FAQ's are very welcome.

I had a real mental block about hashes of hashes, arrays of hashes, arrays of arrays -- and all because they use references.

It turns out they're actually quite easy.

Here's a short script that demonstrates how to group related data together in a sensible structure.

# how to use an hash of hashes
use strict;
use warnings;

my (%hash, $key);
my ($i, $fname, $lname, $mental_age);

# reads data from after __END__ in this file

# get a line of data
  next if /^\s+$/;
  my ($i, $fname, $lname, $mental_age) = split(/, /);

# creates a reference to an anonymous hash
#  and stores that in the %ary hash, indexed by $i
  $hash{$i}= {
    FName => $fname,
    LName => $lname,
    MAge => $mental_age

foreach $key (keys %hash){       # loop thru the hash
  $fname = $hash{$key}{FName};   # retrieve the firstname
  $lname = $hash{$key}{LName};   # retrieve the last name
  $mental_age = $hash{$key}->{MAge};  # mental age of subject
  print &quot;:$key:$fname $lname ($mental_age):\n&quot;;

01, Mike, Lacey, one
02, Kim, Lacey, forty
03, George, Lacey, four
What's an anonymous hash?
It's an ordinary hash, but without a name you create one as I've done above:
        $some_scalar_variable = {
            FName => $fname,
            LName => $lname,
        MAge => $mental_age
You can put it all on one line if you like, I just find it easier to read like this. The bit in blue is the part that creates the anonymouse hash.

The scalar variable (in green) is the reference. The nice thing about a reference is that you can put one in a list, either a hash as I've done here or a scalar array.

The Perl documentation says that you have to dereference the reference in order to use it - and that's part of what I found difficult. But it's not difficult, in the example above I just treated whatever was in the current element of %hash as if it were another hash.

I'd appreciate any comments you might have about this FAQ.

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close