How-to: Perl Hashes and Hashes of Hashes

Perl Hashes and Hashes of Hashes

Introduction to Perl Hashes

In this tutorial, we will cover Perl Hashes and Hashes of Hashes. Perl Hashes are a great way to store key => value pairs. Perl Hashes are stored in running memory so accessing the data stored in them is very fast. In more advanced cases you can pull data from a database and cache it in a hash for quick access to the needed data. Sometimes programmers will use arrays for this purpose however programs and scripts can become even more powerful (and faster) when you start using Perl Hashes of Hashes vs arrays.

Following along with this guide you will learn how Perl Hashes work and give some real world examples of how you might use a Perl Hash or a Hash of Hashes for your own projects.
[adrotate banner=”3″]

Basic Perl Hashes

Lets start simple for our first example. We will create a small Perl Hash called $person that will store the following keys: firstName, lastName, age. The values we will set as a person called John Doe who is 42 years old. Then we will use Data::Dumper to print out whats stored in the hash.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $person = {firstName=>'John', lastName=>'Doe', age=>'42'};

print Dumper($person);

This will give you an output like:

$VAR1 = {
          'firstName' => 'John',
          'lastName' => 'Doe',
          'age' => '42'
        };

Referencing Perl Hash Elements

Now lets say you want to reference on key value pair in the Perl Hash. In this example we will show you how to print our persons Name and age using direct hash references.

#!/usr/bin/perl

use strict;
use warnings;

my $person = {firstName=>'John', lastName=>'Doe', age=>'42'};

print "$person->{'firstName'} $person->{'lastName'} is $person->{'age'} years old.\n";

When this script is run, it will print out the following:

#>./testhash.pl
John Doe is 42 years old.
#>

[adrotate banner=”3″]

Building a Perl Hash of Hashes

Now lets get a little more complex. For the next example, we are going to read in a file with a few “people records” in it. Then we will build a Perl Hash of Hashes to store each persons data, and then finally print the Perl Hash using both Data::Dumper and individual hash element references.

First create a file called lists.txt and put some data in this format inside:

ID=1 First=John Last=Doe AGE=42
ID=2 First=Jane Last=Doe AGE=35
ID=3 First=Jack Last=Doe AGE=17
ID=4 First=Jill Last=Doe AGE=15

Next we will build the script to read this file, split the data, and then split each element down to just the data we need to populate each Hash inside of the main Hash called ‘people’.

#!/usr/bin/perl

use Data::Dumper;
use strict;
use warnings;

getPeople();

sub getPeople {
        my $file = 'list.txt';
        my $people;
        open( LIST, "< $file" ) or die "Can't open $file : $!";
        while (my $row = <LIST> ) {
                my ($id, $first, $last, $age) = (split /[\s=]/, $row)[1, 3, 5, 7];
                $people -> { $id } = {
                                id => $id,
                                first => $first,
                                last => $last,
                                age => $age
                                };

}
print Dumper($people);
print "The person with ID 3 is $people->{3}{first} $people->{3}{last}\n";
close LIST;
}

If everything went well you will get an output like this:

#>./testhash.pl

$VAR1 = {
          '4' => {
                   'first' => 'Jill',
                   'last' => 'Doe',
                   'id' => '4',
                   'age' => '15'
                 },
          '1' => {
                   'first' => 'John',
                   'last' => 'Doe',
                   'id' => '1',
                   'age' => '42'
                 },
          '3' => {
                   'first' => 'Jack',
                   'last' => 'Doe',
                   'id' => '3',
                   'age' => '17'
                 },
          '2' => {
                   'first' => 'Jane',
                   'last' => 'Doe',
                   'id' => '2',
                   'age' => '35'
                 }
        };
The person with ID 3 is Jack Doe

[adrotate banner=”3″]

Conclusion

You can build upon this and the possibilities for using Perl Hashes or Hashes of Hashes are endless. You can also continue to add more dimensions to your Perl Hashes as you see fit. Using Perl Hashes and Perl Hashes of Hashes are really powerful ways to store and reference data quickly.

For additional reading on hashes be sure to check out our previous article on using Anonymous Perl Hashes.  You can also read more over at perl.org’s perldoc site .

Happy Hashing!

-Tutor

 

 

 


Posted

in

,

by

Tags:

Comments

One response to “How-to: Perl Hashes and Hashes of Hashes”

  1. […] The list of users is in the same format as from our Perl Hash Tutorial.  […]

Leave a Reply

Your email address will not be published. Required fields are marked *