Associative Arrays


Ordinary list arrays allow us to access their element by number. The first element of array @food is $food[0]. The second element is $food[1], and so on. But Perl also allows us to create arrays which are accessed by string. These are called associative arrays.To define an associative array we use the usual parenthesis notation, but the array itself is prefixed by a % sign. Suppose we want to create an array of people and their ages. It would look like this:
%ages = ("Michael Caine", 39, "Dirty Den", 34, "Angie", 27, "Willy", "21 in dog years", "The Queen Mother", 108);
Now we can find the age of people with the following expressions

$ages{"Michael Caine"}; # Returns 39$ages{"Dirty Den"}; # Returns 34$ages{"Angie"}; # Returns 27$ages{"Willy"}; # Returns "21 in dog years"$ages{"The Queen Mother"}; # Returns 108
Notice that like list arrays each % sign has changed to a $ to access an individual element because that element is a scalar. Unlike list arrays the index (in this case the person's name) is enclosed in curly braces, the idea being that associative arrays are fancier than list arrays.
An associative array can be converted back into a list array just by assigning it to a list array variable. A list array can be converted into an associative array by assigning it to an associative array variable. Ideally the list array will have an even number of elements:
@info = %ages; # @info is a list array. It # now has 10 elements$info[5]; # Returns the value 27 from # the list array @info%moreages = @info; # %moreages is an associative # array. It is the same as %ages
Operators

Associative arrays do not have any order to their elements (they are just like hash tables) but is it possible to access all the elements in turn using the keys function and the values function:

foreach $person (keys %ages){ print "I know the age of $person\n";}foreach $age (values %ages){ print "Somebody is $age\n";}
When keys is called it returns a list of the keys (indices) of the associative array. When values is called it returns a list of the values of the array. These functions return their lists in the same order, but this order has nothing to do with the order in which the elements have been entered.
When keys and values are called in a scalar context they return the number of key/value pairs in the associative array.There is also a function each which returns a two element list of a key and its value. Every time each is called it returns another key/value pair:
while (($person, $age) = each(%ages)){ print "$person is $age\n";}
Environment Variables

When you run a perl program, or any script in UNIX, there will be certain environment variables set. These will be things like USER which contains your username and DISPLAY which specifies which screen your graphics will go to. When you run a perl CGI script on the World Wide Web there are environment variables which hold other useful information. All these variables and their values are stored in the associative %ENV array in which the keys are the variable names. Try the following in a perl program:

print "You are called $ENV{'USER'} and you are ";print "using display $ENV{'DISPLAY'}\n";Subroutines


Like any good programming langauge Perl allows the user to define their own functions, called subroutines. They may be placed anywhere in your program but it's probably best to put them all at the beginning or all at the end. A subroutine has the form

sub mysubroutine{ print "Not a very interesting routine\n"; print "This does the same thing every time\n";}regardless of any parameters that we may want to pass to it. All of the following will work to call this subroutine. Notice that a subroutine is called with an & character in front of the name:
&mysubroutine; # Call the subroutine&mysubroutine($_); # Call it with a parameter&mysubroutine(1+2, $_); # Call it with two parameters
Parameters

In the above case the parameters are acceptable but ignored. When the subroutine is called any parameters are passed as a list in the special @_ list array variable. This variable has absolutely nothing to do with the $_ scalar variable. The following subroutine merely prints out the list that it was called with. It is followed by a couple of examples of its use.
sub printargs{ print "@_\n";}&printargs("perly", "king"); # Example prints "perly king"&printargs("frog", "and", "toad"); # Prints "frog and toad"Just like any other list array the individual elements of @_ can be accessed with the square bracket notation:
sub printfirsttwo{ print "Your first argument was $_[0]\n"; print "and $_[1] was your second\n";}Again it should be stressed that the indexed scalars $_[0] and $_[1] and so on have nothing to with the scalar $_ which can also be used without fear of a clash.
Returning Values

Result of a subroutine is always the last thing evaluated. This subroutine returns the maximum of two input parameters. An example of its use follows.
sub maximum{ if ($_[0] > $_[1]) { $_[0]; } else { $_[1]; }}$biggest = &maximum(37, 24); # Now $biggest is 37The &printfirsttwo subroutine above also returns a value, in this case 1. This is because the last thing that subroutine did was a print statement and the result of a successful print statement is always 1.
Local Variables

The @_ variable is local to the current subroutine, and so of course are $_[0], $_[1], $_[2], and so on. Other variables can be made local too, and this is useful if we want to start altering the input parameters. The following subroutine tests to see if one string is inside another, spaces not withstanding. An example follows.
sub inside{ local($a, $b); # Make local variables ($a, $b) = ($_[0], $_[1]); # Assign values $a =~ s/ //g; # Strip spaces from $b =~ s/ //g; # local variables ($a =~ /$b/ || $b =~ /$a/); # Is $b inside $a # or $a inside $b?}&inside("lemon", "dole money"); # trueIn fact, it can even be tidied up by replacing the first two lines with
local($a, $b) = ($_[0], $_[1]);




› See More: Perl Tutorial: A Basic Program IV