next up previous contents
Next: Associative Array Operators Up: Arrays Previous: Some Useful Array Functions

Associative Arrays

Associative arrays are a very useful and commonly used feature of Perl.

Associative arrays basically store tables of information where the lookup is the right hand key (usually a string) to an associated scalar value. Again scalar values can be mixed ``types''.

We have already been using Associative arrays for name/value pair input to CGI scripts.

Associative arrays are denoted by a verb| When you declare an associative array the key and associated values are listed in consecutive pairs.

So if we had the following secret code lookup:

name code
dave 1234
peter 3456
andrew 6789
We would declare a Perl associative array to perform this lookup as follows:

%lookup = ("dave", 1234, 
           "peter", 3456, 
           "andrew", 6789);

The reference a particular value you do:


You can create new elements by assignments to new keys. E.g.

$lookup{"adam"} = 3845;

You do new assignments to old keys also:

# change dave's code
$lookup{"dave"} = 7634;