A.7 Hashes

A hash (also called an associative array) is a collection of zero or more pairs of scalar values, called keys and values. The values are indexed by the keys. An array variable begins with the % sign followed by a legal variable name. For instance, possible hash variable names are:

%hash1
%genes_by_name

You can assign a value to a key with a simple assignment statement. For example, say you have a hash called %baseball_stadiums and a key Phillies to which you want to assign the value Citizens Bank Park. This statement accomplishes the assignment:

$baseball_stadiums{'Phillies'} = 'Citizens State Bank';

Note that a single hash value is referenced by a $ instead of a % at the beginning of the hash name; this is similar to the way you reference individual array values using a $ instead of a @.

You can assign several keys and values to a hash by placing their scalar values in a list separated by commas and surrounded by a pair of parentheses. Each successive pair of scalars becomes a key and a value in the hash. For instance, you can assign a hash the empty list:

%hash = (  );

You can also assign one or more scalar key/value pairs:

%genes_by_name = ('gene1', 'AACCCGGTTGGTT', 'gene2', 'CCTTTCGGAAGGTC');

There is an another way to do the same thing, which makes the key/value pairs more readily apparent. This accomplishes the same thing as the preceding example:

%genes_by_name = (
    'gene1' => 'AACCCGGTTGGTT',
    'gene2' => 'CCTTTCGGAAGGTC'
);

To get the value associated with a particular key, precede the hash name with a $ and follow it with a pair of curly braces containing the scalar value of the key:

$genes_by_name{'gene1'}

This returns the value 'AACCCGGTTGGTT', given the value previously assigned to the key 'gene1' in the hash %genes_by_name. Figure A-2 shows a hash with three keys.

Figure A-2. Schematic of a hash
figs/mpb_aa02.gif

You can get an array of all the keys in a hash with the operator "keys", and you can get an array of all the values in a hash with the operator "values".

The arrays you get won't be sorted. Here's an example:

%h = ( 'one'         => 'for the money',
       'two'         => 'for the show',
       'three'       => 'to get ready'
);
@keys = keys %h;
@values = values %h;
print "@keys\n@values\n";

gives the output:

three one two

to get ready for the money for the show.

You can make a reference to a hash by preceding it with a backslash; you dereference it by preceding the reference with a percent sign % for the entire hash or with an extra dollar sign $ for an individual value of some hash key:

%h = ( 'one'         => 'for the money',
       'two'         => 'for the show',
       'three'       => 'to get ready'
);
$href = h;
print $href, "\n";
print $$href{'two'}, "\n";
foreach $key ( keys %$href ) {
   print "key = $key         value = $$href{$key}\n";
}

gives the output:

HASH(0x811d1e0)
for the show
key = three    value = to get ready
key = one      value = for the money
key = two      value = for the show

You can also define a hash as an anonymous hash. An anonymous hash isn't saved in a named hash variable; it's a reference to hash data, and can only be saved in a reference. It is initialized within curly brackets:

$anonhash = { 'one' => 'first',
              'two' => 'second',
              'three' => 'third'
            };
foreach $key ( keys %$anonhash ) {
   print "key = $key         value = $$anonhash{$key}\n";
}

gives the (unsorted) output:

key = three   value = third
key = one     value = first
key = two     value = second