
A hash (associative array) is a structure that stores data in the form of key/value pairs. The keys are unique strings, and the values can be any scalar value (string, number, or reference). In essence, a hash is a look-up table, or, in some people’s terms, a dictionary.
Hashes have a % prefix. You can assign a simple list to a hash and it will be interpreted as sequential key/value pairs. Consider a set of translations of color names from English to French:
my %color_map = ('red', 'rouge', 'blue', 'bleu', 'green', 'vert');
In the above hash the string ‘red’ is the key for the value ‘rouge’. Perl gives us an alternative to the comma that is particularly useful in making hash assignments more readable — this is the ’=>’ or fat-comma:
my %color_map = ( red => 'rouge', blue => 'bleu', green => 'vert');
The => operator is a synonym for an ordinary comma with one extra feature: It automatically quotes the string on the left. The second example makes it much more clear that we are assigning 3 associations to the hash rather than a 6-element list. You can access individual elements of a hash using the $ symbol and using curly braces to denote which key you want to access:
print "$color_map{red}\n"; # prints: rouge
Now, consider a simple program that asks a user to input a color name and which then prints out the French translation. Without hashes you will probably wind up using a series if if/elsif tests to discover what color was entered and print out the appropriate translation. However, with the %color_map hash we need use a single if/else construct to see if the word has a translation in our hash and act accordingly:
my %color_map = ( red => 'rouge',
blue => 'bleu',
green => 'vert',
black => 'noir',
purple => 'violet',
yellow => 'jeune',
white => 'blanc',
brown => 'brun',
grey => 'gris',
);
print "Enter a color in English: ";
chomp( my $color = <STDIN>);
$color = lc($color);
if ( $color_map{$color} ) {
print "$color translates to '$color_map{$color}'\n";
} else {
print "No translation available for '$color'\n";
}
One thing to notice is that keys in a hash are case-sensitive, hence we used the lc() function to lowercase the user’s input before we looked up the color in the hash.
An important distinction between hashes and arrays are that hashes are not ordered — in the above example the key ‘red’ is not the first element in the hash. We can use the keys() function to retrieve the list of keys in a hash, and the values() function to retrieve the list of values. To walk through a hash and print out each pairing you can simply iterate over the keys:
foreach my $key (keys %hash) {
print "$key : $hash{$key}\n";
}
The each() function returns key/value pairs in scalar context so you may also iterate over the hash like so:
while( my($key, $val) = each(%hash) ) {
print "$key : $val\n";
}
A couple of earlier articles describe ways of using hashes and you might want to look them up in the archive (and archive link is provided below in the resources section) — see the articles dated July 20, 2000 and August 31, 2000.
*****