In perl, a hash is differentiated from other data types with the '%'-sign. A hash is essentially an array whose elements are named. This makes it easy to access any specific element given that name. In common perl parlance, the element's name is called the "key" and the element is called the "value".

As mshumphr demonstrates above, a hash element is accessed with the '$'. There are two basic forms to dereference the data:

$value = $hash{key}
$value = $hash->{key}

In older per documentation, Hashes used to be called "associative arrays".

For more information, see the perldata manpage.

The hash is one of the three basic data types with in the perl language. In the simplest form, the hash is designated with the '%' sign (often used as the mod operator within C-like languages and thus a critical part of any hash implementation) and accessed with the curly braces: '{' and '}'.

# init
%foo = (
    'a' => 1,
    5   => 'qux'
  );

print $foo{'a'},"\n";
1
print $foo{5},"\n";
qux

Often called an 'associative array', the hash is used to associate one data type with some value. The key point with the hash is that this does not have to be ordinal or enumerated to access. Arrays must be indexed with numbers or objects that have a clear order to them - for example '0, 1, 2, ...'. It is impossible to index an array with real numbers or strings - how many real numbers are there between '3.1' and '3.2'? Or how many strings between 'accuse' and 'ace'? There is no clean, programmatic answer for these questions and thus the hash table is the best choice for using these objects as an index into the data structure.

With an array, to have two elements '1' and '1000', there needs to be storage allocated for all 1000 elements. The equivalent hash is much smaller (often two storage units with a bit of buffer for more if necessary). In this way, the hash can be used as a sparse array for when most of the elements are '0' or empty with a very few items spread out over vast distances.

Another common use for hashes is counting elements or identifying all of the unique items - such as the following snipit demonstrates:

%counter;
$words = "How much wood would a woodchuck chuck " . 
         "if a woodchuck could chuck wood";
foreach $word (split(/\s+/,$words))
{
  $counter{$word}++;
}
foreach $word (keys %counter)
{
  print "$word: $counter{$word}\n";
}

a: 2
wood: 2
would: 1
much: 1
chuck: 2
How: 1
could: 1
if: 1
woodchuck: 2

keys takes a hash (such as %counter) and returns a list (or array if you prefer to think that way) of the indexes to the hash. Another approach is the use of each which will iterate through the hash returning (key,value) pairs.

With perl5, the addition of references has both complicated and simplified the issues. First off, this allows for deeper data structures such as hashes of hashes and multi-dimensional arrays (which weren't supported cleanly prior to perl5). Secondly, it means that it is now much easier to pass a hash (and especially multiple hashes) to and from a subroutine. To make a reference, simply put a '\' before the name of the hash (or array, or even at times scalar). The resulting value is a scalar:

%foo = ('a' => 5, 'b' => 2);
$bar = \%foo;
Accessing a hash reference is slightly different than that of a hash, the '->' operator comes into play.
print $bar->{'a'},"\n";
5
While the '->' can be used to access individual items of the array, to use either keys or each the hash must be dereferenced. If this is a simple hash reference (like $bar above), just placing the % before it to have '%$bar' will allow the function to access the hash reference as a hash. For hashes within other data structures, is necessary to cleanly designate the entire structure:
%foo = ('a' => 5, 'b' => 2);
@bar;
$bar[0] = \%foo;
print keys %{$bar[0]};
a b
(First the hash was set up, then the array was defined, and array element of 0 was assigned the value of the hash reference. Lastly the array element of 0 was accessed, and this scalar value dereferenced into a hash which was processed by keys and then printed.)

Log in or registerto write something here or to contact authors.