< BACKCONTINUE >

8.1 Hashes

There are three main datatypes in Perl. You've already seen two: scalar variables and arrays. Now we'll start to use the third: hashes (also called associative arrays).

A hash provides very fast lookup of the value associated with a key. As an example, say you have a hash called %english_dictionary. (Yes, hashes start with the percent sign.) If you want to look up the definition of the word "recreant," you say:

$definition = $english_dictionary{'recreant'};

The scalar 'recreant' is the key, and the scalar definition that's returned is the value. As you see from this example, hashes (like arrays) change their leading character to a dollar sign when you access a single element, because the value returned from a hash lookup is a scalar value. You can tell a hash lookup from an array element by the type of braces they use: arrays use square brackets [ ]; hashes use curly braces { }.

If you want to assign a value to a key, it's similarly an easy, single statement:

$english_dictionary{'recreant'} = "One who calls out in surrender.";

Also, if you want to initialize a hash with some key-value pairs, it's done much like initializing arrays, but every pair becomes a key-value:

%classification = (
    'dog',      'mammal',
    'robin',    'bird',
    'asp',      'reptile',
);

which initializes the key 'dog' with the value 'mammal', and so on. There's another way of writing this, which shows what's happening a little more clearly. The following does exactly the same thing as the preceding code, while showing the key-value relationship more clearly:

%classification = (
    'dog'   => 'mammal',
    'robin' => 'bird',
    'asp',  => 'reptile',
);

You can get an array of all the keys of a hash:

@keys  = keys %my_hash;

You can get an array of all the values of a hash:

@values  = values %my_hash;

You use hashes in lots of different situations, especially when your data is in the form of key-value or you need to look up the value of a key fast. For instance, later in this chapter, we'll develop programs that use hashes to retrieve information about a gene. The gene name is the key; the information about the gene is the value of that key. Mathematically, a Perl hash always represents a finite function.

The name "hash" comes from something called a hash function, which practically any book on algorithms will define, if you've a mind to look it up. Let's skip the details of how they work under the hood and just talk about their behavior.

< BACKCONTINUE >

Index terms contained in this section

% (percent sign)
      for hash names
[] (brackets)
      array element lookups
{} (curly braces)
      hash lookups, use in
arrays
      associative
      of hash key/value pairs
associative arrays
brackets []
hashes
      initializing
initializing
      hashes with key-value pairs
key/value pairs
      in hashes
percent sign
values, hashes

© 2002, O'Reilly & Associates, Inc.