Using Associative Arrays in Perl Programming

How to Use a Hash in A Perl Application

© Mark Alexander Bain

Jul 19, 2009
Using Associative Arrays in Perl Programming, Mark Alexander Bain
The Perl Associative array (or hash) allows the programmer to use a string as in index to an array rather than a number.

Arrays are useful data structures in every programming language, and in programming languages such as Perl they are particularly easy to work with. However, there are limitations to the traditional array. They are suitable for ordered data (such as the days of the week or the months of the year) but there can be problems when the data has no such defined order (such as an array of physical constants).

In this situation the programmer must come up with different solutions for organising the data. Fortunately there is a simple solution for the Perl programmer and that's associative arrays.

Moving from Arrays to Associative Arrays

Before looking at associative arrays (or hashes) it's worth looking at ordinary arrays. Take, for instance, an array containing the days of the week:

@days = ('Junk', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thurday','Friday','Saturday');

In this example 'Junk' is not an actual day of the week. That has been inserted so that 'Sunday' has the index number of 1 (all Perl arrays start with an index number of 0). The order of the week is then quite obvious and easy to access, for example:

print $days[2];

would, as expected, print "Monday". However, not all lists have such an obvious structure. Take, for instance, an array containing the masses of electrons, neutrons and protons:

@masses = (1.6726 * 10 ** -27, 1.649 * 10 ** -27, 9.1094 * 10 ** -31);

The obvious question at this point is which is which? Is the order actually electron-> neutron->proton or some other order? There is no obvious way of telling. The programmer could, of course, create a second array containing the correct order:

@particle = ('proton', 'neutron', 'electron');

They would then need some means of using the second array to look up the appropriate value from the first:

sub mass {
for (my $i=0; $i<@particle; $i++) {
if ($_[0] eq $particle[$i]) {return $masses[$i]}
}
}
print mass('neutron') . "\n";

And, while this works effectively, it is much simpler to start using associative array, and that's because each value is associated with a string.

Creating an Associative Array with Perl

A Perl associative array is identified by the % (percentage) prefix, and its contents consist of a set of identifiers and associated values:

%physical_constants = (
'speed of light' => 2.997925 * 10 ** 8,
'mass of a proton' => 1.6726 * 10 ** -27,
'mass of a neutron' => 1.649 * 10 ** -27,
'mass of an electron' => 9.1094 * 10 ** -31,
'charge of a proton' => 1.6022 * 10 ** -19,
'universal gravitational constant' => 6.6726 * 10 ** -11,
'gravitational acceleration on Earth' => 9.80665,
'Planck constant' => 6.6022 * 10 ** -34
);

Now rather than having to handle two arrays, the programmer has one concise set of data.

Accessing the Contents of a Perl Associative Array

Once the data has been placed in an associative array the programmer can access individual elements:

print $physical_constants{'speed of light'} . "\n";
print $physical_constants{'Planck constant'} . "\n";

or they can output all of the contents of the array by examining the stored identifiers and values:

while (($key, $value) = each (%physical_constants)) {
print "$key = $value\n";
}

By using this method the Perl programmer has a very simple yet effective way of storing and accessing sets of data within their applications.


The copyright of the article Using Associative Arrays in Perl Programming in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Using Associative Arrays in Perl Programming in print or online must be granted by the author in writing.


Using Associative Arrays in Perl Programming, Mark Alexander Bain
Figure 1: The Perl Associative Array, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo