Associate Arrays in Perl

A lot of people I've met seem to have problems with associative arrays, yet in their scripts they're using them all the time. They aren't difficult to understand, let's get started:

%people = ("Bert", "happy",
    "Bob", "selfish",
    "Joe", "depressed",
    "Dick", "just hangs around",
    "Tez_Tickle", "always around dick"
);

Unlike list arrays, associative arrays start with a %. They are just what they say they are, associative. Look how the array declaration is layed out, in groups of two, bert - happy; bob - selfish; joe - depressed. Each pair is associated with one another, they are separate array items, yet they are tied to each other.

$people{'Bert'}; # Returns "happy"
$people{'Bob'}; # Returns "selfish
$people{'Joe'}; # Returns "depressed"
$people{'Dick'}; # Returns "just hangs around"
$people{'Tez_Tickle'}; # Returns "always around dick"

Look at the top one $people{'Bert'};. Bert is the "key". "happy" is the "value". Each key and value are associated with eachother. Notice how we use $ instead of % when accessing each value, that's because the value is a scalar value, the same as when we access an item in a list array.

We can convert an associative array to a list array, and a list array to an associative array.

@peeps = %people; # @peeps now contains items from %people.
# @peeps contains ten items.
@peeps = ("Bert",
"happy",
"Bob",
"selfish",
"Joe",
"depressed",
"Dick",
"just hangs around",
"Tez_Tickle",
"always around dick");

%more_people = @peeps; # Same as %people.

See the difference? In a list array the items are on their own, and accessed by their number along the array. In an associative array each pair is linked.

Operators

Remember associative arrays have "keys" and "values". The key is used to access the value, look below to see a way we can go through each pair.

foreach $person (keys %people)
{
    print "I know a characteristic of $person\\n";
}

foreach $characteristic (values %ages)
{
    print "Somebody is $characteristic\\n";
}

There is also the function each() that returns a two element list of a key and it's value from an associative array.

while(($person, $characteristic) = each(%people)) {
    print "$person is $characteristic\\n";
}

($key, $value) = each(%people);
# The key is stored in $key, the value stored in $value, each time each() is
# called it returns the next pair.

The while() example above will keep calling each() to return a pair of associative array items into $people and $characteristic and print them out until it has gone through the entire array, then the script will continue.

Contrary to what some people will tell you, associative arrays are simple to use, very handy and understanding them will help you when using:

Environment Variables

Environment variables are readily available to you in your scripts. When the script is run Perl collects information about the person using the script and stores them in the %env variable. Say you're using Perl for CGI on your site.

$ENV{'REMOTE_ADDR'}; #Contains users IP address.
$ENV{'HTTP_USER_AGENT'}; #Contains users user-agent string.
$ENV{'HTTP_REFERER'}; #Contains users referring document.
$ENV{'HTTP_COOKIE'}; #Contains users cookies for your site.

Why not write a little script that goes through each pair in the %env array and print them out, run the script and see what information the script gathered from you.