Beginning CGI Programming with Perl

Exercise 2.3. Using Perl’s associative array

The associative array is the third major Perl data storage container (scalars and regular arrays are the other two). It’s one of Perl’s powerful characteristics and is different from most other language storage types. In this exercise, you’ll learn how to use the associative array, and you’ll look at the difference between a regular array and an associative array. Listing 2.5 shows an associative array.

Listing 2.5. Using associative arrays.

01: #!/usr/local/bin/perl02:03: %names = ("FIRST", "Jessica ", "MIDDLE", "Ann ", "LAST", "Herrmann");04: $full_name{'first'} = "Steven ";05: $full_name{'middle'} = "Michael ";06: $full_name{'last'} = "Herrmann";07:08: print "Howdy my name is $names{'FIRST'}";09: print "$names{'MIDDLE'}$names{'LAST'}\n";10:11: print "And my mixed up name is ";12: foreach $name (keys(%full_name)){13:     print "$full_name{$name}";14: }15: print "\n";

Associative arrays might be a little confusing to start with. Probably more so if you have done any programming. So here is a feature where everyone who is new to programming gets a head start on the experienced gang.

Associative arrays always begin with a percent sign (%). The associative array is similar to the array you learned about in Exercise 2.2, except that the array cell is identified by a string value instead of an integer value.


Identifying an array cell is called indexing into the array.

There are more differences between regular arrays and associative arrays, but the way you index into them is the most important one. You index into a regular array like this:


You index into an associative array by referencing the array cell like this:


Line 8 in Listing 2.5 is a good example of indexing into an associative array. Each array cell of the %names array is referenced by using the array cell name. The names of the cells of the %names array are FIRST, MIDDLE, and LAST. The values placed in each named cell of the %names array are Jessica, Ann, and Herrmann. Just because I like repeating myself, let me restate that: FIRST is the name of the array cell of the %names array; the value, or data, stored into that array cell is the string “Jessica“. The associative array is referred to by using the percent sign (%) like this:


and a cell in the array is referred to by using the dollar sign like this:


When you try Listing 2.5 for this exercise, note the different ways illustrated on lines 3-6 to put information into an associative array



Putting information into any variable is called assigning data to that variable or storing data in a variable


Now take some time to look at the way Listing 2.5 assigns data to the associative arrays %names and %full_name. On line 3, the associative array %names is assigned three values and three array cell names. Array cell names and array cell values must be paired together, as shown on line 3. Line 3 performs the same work on the %names array as lines 4-6 perform on the %full_name array. Each array cell name and value pair on line 3 (FIRST, Jessica, for example) is equivalent to one of the assignments on lines 4-6 ($full_name{'first'} = "Steven ", for example).

On line 4, a new cell named 'first' is created in the associative array %full_name, and the value "Steven" is placed or stored in that array cell. Lines 5-6 just repeat the process.

Line 8 shows you one common way of getting the data out of associative arrays. Each array cell is referenced by its array cell name. The single quotation marks around the array cell name help keep Perl from getting confused about where the print command ends. If you used double quotation marks here, Perl would try to match them up with the previous double quotation marks that match the beginning of the print command. You must use single quotation marks (‘) or double quotation marks (“) when using a string to name the array cell. Otherwise, Perl tries to interpret the name as some type of Perl command. Instead of using a string to name the cell, line 13 uses a variable to name it.

Lines 11-14 are part of a programming construct called loops. Each line is repeatedly executed by the computer based on the conditions set on line 11. You’ll learn about loops in Tutorial 3, “Using Server Side Include Commands.” On line 12, each name of the associative array cells is returned by the function keys.

Perl provides a special function called keys() to retrieve the names or keys to each array cell of associative arrays. The keys() function is used on line 12 as part of the foreach statement: keys(%full_name). You will learn about using or calling functions and subroutines in Tutorial 5 “Decoding Data Sent to Your CGI Program.” The keys(%array_name) function uses the %array_name inside the parentheses and gives back (returns) the name of each array cell in the %array_name passed to it.


Using a subroutine or function is known as calling the subroutine.

The value between the parentheses after the subroutine name is called a parameter. This is often referred to as passing the parameter or data to the subroutine

When the subroutine gives back information, it is returning data..

The returned data is saved in the variable $name. This is the name of an array cell of %full_name. So now you can use this array cell name to retrieve the value from the array cell. This is done on line 13:

print "$full_name{$name}";

The array cell is referenced by using the array name $full_name and then enclosing the array cell name in curly braces ({})-for example, {$name}. This looks like $full_name{$name} when you put it all together.

So line 11 prints And my mixed up name is. Line 12 gets each of the names of the associated array cells in %full_name. Line 13 prints the value of each of the array cells.

When you complete this exercise and run it on your computer, you will see that the names might not come out in order for lines 11-14. This happens because data is stored into associative arrays for efficiency. You cannot count on the original order of assigning data to the array to be the order in which the data is retrieved from the associative array.

This is the other main difference between associative arrays and regular arrays. Because regular arrays are indexed by numbers, the data usually is stored sequentially and always can be retrieved sequentially.

Because the associative array’s data is stored in association with strings, it cannot be retrieved in the same order in which it was stored. You always will be able to retrieve the data using the array cell name, however.

Leave a Reply

Your email address will not be published. Required fields are marked *