HTML CSS PHP PERL

Getting Data Out of a Hash

 
Previous Table of Contents Next

Getting Data Out of a Hash

As you have seen, to retrieve a single value from a hash, simply use a $, the name of the hash, and (in curly braces) the key whose value you want to retrieve. Now consider this example:

%Movies = ( 'The Shining' => 'Kubrick', 'Ten Commandments' => 'DeMille', Goonies => 'Spielberg'); print $Movies{'The Shining'};

These lines print the value associated with the key The Shining in the hash %Movies. This example would print Kubrick.

Sometimes examining all the elements of a hash is useful. If all the keys of the hash are known, you can access them individually by key as shown previously. Most of the time, though, accessing each key by name isn't convenient. Some of the key names might not be known, or the keys may be too numerous to enumerate individually.

You can use the keys function to retrieve all the keys of a hash returned as a list. That list can then be examined to find all the elements of the hash. The keys of the hash aren't stored in any particular order internally, and the keys function doesn't return them in any particular order. To print all the movies in this hash, for example, you could use the following:


foreach $film (keys %Movies) {

    print "$film\n";

}


Here, $film takes on the value of each element of the list returned by keys %Movies. If you want to print all the directors' names in addition to the movie titles, you can enter the following:


foreach $film (keys %Movies) {

    print "$film was directed by $Movies{$film}.\n";

}


This snippet might print the following output:


Ten Commandments was directed by DeMille.

The Shining was directed by Kubrick.

Goonies was directed by Spielberg.


Because $film contains the value of a hash key, $Movies{$film} retrieves the value of the element of the hash represented by that key. You can print both of them to see the key-value relationship in the hash. (Remember that your output might appear in a different order, because the keys are returned in no particular order by the keys function.)

Perl also provides the values function to retrieve all the values stored in a hash. Retrieving the values alone usually isn't useful, because you can't tell which key is associated with which value. The values of a hash are returned in the same order as the keys function would return the keys. Now consider the following example:


@Directors=values %Movies;

@Films=keys %Movies;


Here, each subscript of @Directors and @Films contains a reference to the same key-value pair from %Movies. The name of the director contained in $Directors[0] corresponds to the name of the movie stored in $Films[0], and so on.

Sometimes you need to retrieve individual elements from the hash by value instead of by key. The best method of retrieving by value is to invert the hash. This means that you make a new hash in which all the keys of the original hash become values, and all their values in the original hash become keys in the new hash. The following is an example:

%Movies = ( 'The Shining' => 'Kubrick', 'Ten Commandments' => 'DeMille', Goonies => 'Spielberg'); %ByDirector = reverse %Movies;

What's this? When you use the reverse function on a hash, Perl unwinds the hash into a flat listperhaps something like this:


('The Shining', 'Kubrick', 'Ten Commandments', 'DeMille', 'Goonies', 'Spielberg')


Perl then reverses the order of the elements in the list, and you get the following output:


('Spielberg', 'Goonies', 'DeMille', 'Ten Commandments', 'Kubrick', 'The Shining')


Notice that now all the key-value pairs are switched around (values are now first). When you assign this list to %ByDirector, the resulting hash is identical to the original, except that all the keys are now values and all the values are now keys. Beware, however, if your hash has duplicate values for some reason. If the values (which are to become keys) are not unique, you end up with a hash with fewer elements than you started with. As the duplicate values collide in the new hash, old keys are replaced with the new ones.

    Previous Table of Contents Next
    © 2000- NIV