Perl hash introduction tutorial

Perl hash FAQ: Can you share some simple Perl hash examples?

Sure. If you're not familiar with them, a Perl hash is simply a Perl array that is indexed by a string instead of a number. A Perl hash is like a Map in the Java programming language, or an array in PHP.

Perl hash - Background information

To get started looking at a hash in Perl, let's look at a simple example. First, let's assume that Perl hashes don't exist. Next, lets assume that we need to store the prices of various food items you'll find in a restaurant.

Without a Perl hash, the most logical way to store our restaurant data would be to have two arrays that we keep in sync: one array to store the names of the food items, and a second array to store their corresponding prices. Our arrays would like this:

$food[0] = 'pizza';
$price[0] = 12.00

$food[1] = 'coke';
$price[1] = 1.25

$food[2] = 'sandwich';
$price[2] = 4.00

Keeping two arrays in sync like this allows you to store both the name of the food item, and the price of the food item. You just need to make sure you keep the indexes in sync. For instance, element 0 of the food array would have the name of the food item ('pizza'), and element 0 of the price array would have the corresponding price of that food (12.00).

A simple Perl hash example

Doing something like this with arrays isn't uncommon, and was pretty much all we could do with many languages 15-20 years ago. However, with the concept of a hash in Perl, we can approach this problem differently.

Remember that a Perl hash is just an array that is indexed by a string instead of a number. With that, here's a Perl hash we can create to solve this our current food item and price data storage need:

# create our perl hash
$prices{"pizza"} = 12.00;
$prices{"coke"} = 1.25;
$prices{"sandwich"} = 3.00;

As you can see, we just use the names of our food items as the keys of our Perl hash, and the price of each food item is stored as the value. If you've never seen this before, I think you'll agree this is very cool. (If you have used this Perl hash feature before, you probably use it very often.)

(Note: Because a Perl hash is really just an array, you'll sometimes see a term like "Perl hash array", or "Perl array hash".)

In a Perl hash, the strings we use ('pizza', 'coke', etc.) are referred to as the hash keys, or simply keys; and whatever is stored on the right side of the equal sign in the assignment statements is referred to as the hash values, or simply values. As you can see from the code, each key has an associated value.

A complete Perl hash script

To finish this Perl hash tutorial, here's the source code for a complete Perl script where I first create a Perl hash, and then I loop through all the keys in the Perl hash, and then print each key and value that are contained in the hash:

#!/usr/bin/perl

# create our perl hash

$prices{'pizza'} = 12.00;
$prices{'coke'} = 1.25;
$prices{'sandwich'} = 3.00;

# traverse our perl hash, printing the
# key and value for each array element

print "\nforeach output:\n";
foreach $key (keys %prices)
{
  # do whatever you want with $key and $value here ...
  $value = $prices{$key};
  print "  $key costs $value\n";
}

I hope this example makes sense, I tried to keep it simple. The most important things to note here have to do with the hash syntax, specifically:

  • The use of curly braces around the strings. (Perl arrays that are indexed by numbers use brackets around numbers.)
  • The use of the percent symbol before the name of the hash in the foreach loop.

I also hope the Perl foreach loop shown is a good example of how to iterate through all the keys and values in our hash. Once you create a Perl hash, this is a very common way of iterating through all the elements in the hash.

Once you wrap your head around a Perl hash, you realize it can be a very powerful way of storing associated data. (In fact, hashes used to be referred to as "associated arrays".) The major difference between a Perl hash and a regular Perl array is that the array is indexed by number, and the hash is indexed by a string, but this simple difference makes hashes a very powerful data construct.

More Perl hash tutorials

Here's a link to more Perl hash tutorials using our search form. And as of this writing, here are the Perl hash tutorials that seem most-related to this new hash tutorial:

Related Perl hash tutorials

I hope you found this short Perl hash tutorial helpful. We have many more Perl hash tutorials on this site, including the following:

Getting started Perl hash tutorials:

More advanced Perl hash tutorials:

The hash in Perl - hash sorting tutorials:

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.