Perl Hashes: Build Your Content

perl hashesPerl hashes are one of the most commonly used, powerful features of Perl. Typically, you would use hashes to build a dictionary’s contents, with each dictionary word being a key and the values corresponding being the word definitions.

In Perl, hashes are one of the available structures of data. Perl hashes are sometimes referred to as maps, dictionaries or associative arrays. Basically, a hash is a group of key value pairs that are not in order. The values are scalar values and the keys are unique strings, as you can see in this article about Perl strings. Each of the values can be a reference, a string or a number. Like other variables in Perl, you can declare hashes by using the keyword ‘my.’ The variable names are preceded by the percentage sign (%).

The Difference Between a Perl Hash and an Array

In other languages, such as PHP, there is no difference between a hash and an array, as you can see in this PHP course. This is why some people think they are the same thing. In Perl, however, there are 2 main differences between an array and a hash.

You can access values using a key which is a string in hashes and these are generally not in order, which you can see first-hand in this Perl 5 course. On the other hand, you can access an array using its numerical index and these are in order.

Every hash key is related to one value and inside a single hash structure, the keys are all unique. These means there are no repeating keys allowed.

Hashing Functions

Now that you know that hashes are arrays, keep in mind that they do not require that every key directly corresponds to a unique entry. Rather, functions called ‘hashing functions’ are utilized for calculating the corresponding index to a specific key. Each entry in a hash is called a bucket or a slot. The functions could return the same hash values for 2 keys or more. This means that the index does not really have to be unique.

Creating an Empty Hash


Insert a Pair of Key-Values into a Hash:

In this case, the associated value if ‘red,’ and the key is ‘apple:’


Instead of the key, you can also use variables, which saves you from having to put quotation marks on the variables:


Actually, you can even leave out quotes when directly using the string if it is a simple string:


Here, you can see that when you access a specific pair of key values, you don’t use the % sign. Instead, use the $ sign since you are accessing a single scalar value. You can see that the key has been put into curly braces.

Fetching a Hash Element

You can also fetch an element’s value the same way you insert an element:


The hash returns an ‘undef’ if the key does not exist. If you enable warnings, as you should, you will get a warning about a value that is uninitialized:


You can also add a few pairs of key values:


Using Values to Initialize Hashes

You can instantiate each variable that has a key-value pair as you pass a list of key value pairs to the hash simultaneously:


To indicate elements in pairs, the => is used, which is called the fat comma or the fat arrow. In Perl, there is another thinner arrow used, which looks like this:


Since the fat comma is basically the same as a comma, you can also write the above string this way:


You can also get a more readable, cleaner code when you leave out the left-hand side quotes, which you will be able to do with a fat comma:


Assigning to Hash Elements

Here is what happens when you assign other values to existing keys:


This assignment altered the values associated with the key for apply, keep in mind that each key has a single, unique value.

Iterating Over a Hash

You need to know the key when you want to access values in a hash, which this Perl article tells you more about. When hash keys are not defined, you can use the function keys to get the key list. On these keys you can then iterate:


You can iterate directly over the key functions’ return values. You don’t even need to use the temporary @fruits variable:


Hash Size

When you hear ‘hash size,’ this means the number of pairs of key values. By placing the key functions in scalar context, you can get this:


There you have it, a step by step process on how to create Perl hashes, which you can learn more about with this Perl beginner course.