Perl Crash Course: Hashes

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email


The hash is, in my honest opinion, the most important data structure in Perl. It is nothing more than what other languages call an Associative Array, but it is very powerful and one of the key features of Object Oriented Perl.

If Associative Array still doesn’t ring a bell, then think of a file cabinet. Your every day file cabinet holds files that are identified by tags. These tags are the keys to your files. The content of each file can be considered its value. So the same happens to a hash. It’s an array of key/value pairs. You can identify a hash by the symbol that starts its variable name:


% was chosen to identify hashes because of the similarity to a key/value pair…

Hash keys are unique, and can be made up of any character and be of any size. Depending on your key name, you might need to use it between single or double quotes.

Hash values can be anything you would normally assign to a scalar variable, including anonymous arrays and anonymous hashes (more on this when we see Complex Data Structures).

Interpolation rules apply just like with any other quoting – either for keys or values. Use “=>” (fancy comma) to separate keys from values. Regular commas work too, but are more difficult on the eyes.

%some_hash = (key1=>'val1', "some key with special chars"=>$val2 );# basic hash population
%another = (0 => 'a',   # Key 0 with value 'a'
            1 => 'b', # key 1 with value 'b'
            2 => 'c');# key 3 with value 'c'

When printing a hash, keep in mind that unlike arrays, hashes do NOT keep their keys in the order they were assigned. Perl rearranges hash element orders to maximize performance. If you need to print your keys in a certain order, you will have to sort them when accessing. We’ll see scalar, array, and hash functions in the chapter Gettin’ jiggy wit it further ahead.
The simplest way to access a hash element is to call its key. We use { } to delimit hash keys.

print %some_hash; # prints key1val1
print $some_hash{key1}; # prints val1

Note that we are accessing one element of the hash, and therefore using $ to start the variable name. The $ alone would be confused with a scalar variable $some_hash, so we have to use { } to delimit the key. You’ll get used to seeing this in your or other people’s code:

$var = 'a scalar variable';
$var[0] = 'first element of array @var';
$var{key1} = 'an element of hash %var';

Although the 3 examples above start with $var, they are totally different things. Perl won’t get confused, and neither will you if you learn to look for [ and { after a variable name.

« Arrays and Lists | TOC | References and Complex Data Structures (CDS) »

More To Explore


bbPress Notify (No-Spam) Documentation

bbPress Notify (No-Spam) Documentation The bbPress Notify (No-Spam) is a free plugin that lets you control who gets notifications of new Topics and Replies in

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.