Table of Contents
Introduction to Perl hash
Hash is defined in term of key-values pairs. Perl stores elements of hash in such an optimal way that you can look up values based on keys very efficiently and fast. In array, you use an index to access an element, however in hash you must use an descriptive key. Hash is also known as associative array.
Like a scalar or array variables, hash has it own prefix for its variables. A hash variable must begin with a percent sign (%).
The prefix % looks like key/value, so remember this trick to name the hash variable.
The following example defines a simple hash.
my %countries = qw(England English France French Spain Spanish China Chinese Germany German);

To make the code easier to read, Perl provides operator => as an alternative to comma. It helps differentiate between keys and values and make the code clear that you are dealing with hash instead of list or array. The hash above can be rewritten using operator => as below:
my %countries = ( England => 'English', France => 'French', Spain => 'Spanish', China => 'Chinese', Germany => 'German');
Perl requires the keys of hash must be strings, meanwhile the values can be any scalars. If you use non-string values as keys of hash, you may get unexpected results while processing its elements.
In addition, a hash key must be unique. If you try to add a new key-value pair with the key that is similar to an existing key, the value of the existing key will be over-written. No new element is added.
It is important to notice that you can omit the quotation in the keys of hash which is totally fine even with strict vars in operation.
Look-up hash values
To look up for a hash value, you use hash key inside curly brackets {}. Take a look at the following example:
#!/usr/bin/perl
use warnings;
use strict;
# defines country => language hash
my %langs = ( England => 'English',
France => 'French',
Spain => 'Spanish',
China => 'Chinese',
Germany => 'German');
# get language of England
my $lang = $langs{'England'}; # English
print($lang,"\n");Adding a new element to hash
To add a new element to hash, you use a new key-pair value as follows:
$langs{'Italy'} = 'Italian';Removing a single key/value pair from hash
If you know the hash key, you can remove single key/value pair from hash using delete function as follows:
delete $langs{'China'};Modifying value of a hash element
You can modify value of existing key-value pair by reassign to the new value.
# add new key value pair
$langs{'India'} = 'Many languages';
# modify official language of India
$langs{'India'} = 'Hindi'; #Looping over hash elements
Perl provides keys function so that you can get a list of keys in scalars and use it to loop through a hash:
#!/usr/bin/perl
use warnings;
use strict;
# defines country => language hash
my %langs = ( England => 'English',
France => 'French',
Spain => 'Spanish',
China => 'Chinese',
Germany => 'German');
for(keys %langs){
print("Official Language of $_ is $langs{$_}\n");
}keys() function returns a list of keys. The for loop visits each key and assign it to the variable $_. Inside the loop, we access value of hash element via key using $langs{$_}. You will learn more about the for loop statement in later tutorial.
In this tutorial, you have learned about Perl hash and how to manipulate its elements.