Perl Hashes

Associative Arrays (Hashes):
Hashes are also called associative arrays. I will be using the two terms interchangeably. Hashes are indexed by string values instead of an integer index value. Associative arrays, unlike scalar arrays, do not have a sense of order. There is no first addressable element. This is because the indexes of the hashes are strings and information is not stored in a predictable order.

A hash is best thought of as a two-column table, where the left column stores keys and the right colunm stores their associated scalar values. It's called a hash because a hashing algorithm is used to map each key string to an internal index into the table. To retrieve a value from a hash, you must know the key. If you know a key of hash %hash and you want to print out the value, you would use the following syntax:

print $hash{'mike'};

This example prints out the value of a key named mike in the hash named %hash. The interior of the curly braces (or the left-hand side of a => operator) of the hash will automatically interpret an identifier as a quoted string. So we can also write:

print $hash{mike} # notice that there are no quotes.

This is only true, however, if the contents are an unbroken sequence of alphanumerics or underscores. That is, we can't write:

$sound{mike willis}= "son of willis"; # wrong

if we mean:

$sound{"mike willis"} = "son of willis";

Populating a Hash:

Much like the normal array, an associative array can have all its values assigned at once. The following assign records to the hash %cities:

%cities = ("Toronto" => "East", "Calgary" => "Central", "Vancouver" => "West");
is equivalent to
%cities = ("Toronto", "East" => "Calgary", "Central" => "Vancouver", "West");
is equivalent to
%cities = ("Toronto", "East", "Calgary", "Central", "Vancouver", "West");
is equivalent to
$cities{'Toronto'} = "East";
$cities{'Vancouver'} = "West";
$cities{'Calgary'} = "Central";

Functions:
Data in hashes is stored in key/value pairs. In the example above, Toronto is the key, East is the value. Due to the nature of this assignment (which is unordered), hashes cannot be referenced like an array could. Contents of a hash can be listed by using either of the functions: keys, values, and each.

Keys:
The keys function returns a list of the keys of the given associative array when used in a list context, and the number of keys when used in scalar context.

my %cities = ("Toronto" => "East", "Calgary" => "Central", "Vancouver" => "West");
for $key (keys %cities)
{
print "Key: $key Value: $cities{$key} \n";
}

In scalar context, the keys function gives the number of elements (key-value pairs) in the hash. For example:

if(%cities == 3) {
# do something
}

Values:
The code above returns both keys and its values. If you want only the values of the hash and not the keys, use the values function.

my %cities = ("Toronto" => "East", "Calgary" => "Central", "Vancouver" => "West");
for $value (values %cities)
{
print "Value: $value \n";
}
or
@array = values(%cities);

Each:
The each function iterates over the entire hash and returns all key-value pairs.

my %cities = ("Toronto" => "East", "Calgary" => "Central", "Vancouver" => "West");
while(($key, $value) = each(%cities))
{
print "Key: $key Value: $cities{$key} \n";
}

Delete:
How do you delete an element from a hash? It cannot be done by assigning a null. It can't be done by chopping it off. There is no order, so there is no connection you can chop off. To tend to this need, delete function was created.

delete $cities{"Toronto"};

This will delete the key value pair.
Hash Slices:
Like an array, hashes can also be sliced. Observe:

$cities{"Toronto"} = East;
$cities{"Calgary"} = Central;
$cities{"Vancouver"} = West;

This can be simplified to:

($cities{"Toronto"}, $cities{"Calgary"}, $cities{"Vancouver"}) = ("East", "Central", "West");
or
@cities{"Toronto", "Calgary", "Vancouver"} = ("East", "Central", "West");
or
@locations = qw(Toronto Calgary Vancouver);
print "Places are: @cities{@locations}\n";

Hash slices can also be used to merge smaller hash into a larger one. In this example, the smaller hash takes precedence in the sense that if there are duplicate keys, the value from the smaller hash is used:

%destinations{keys %cities} = values %cities;
or
%destinations = (%destinations, %cities);

The values of %cities are merged into the %destinations hash.