Hashes


A hash (also known as an associative array] is like an array, but it uses strings instead of integers for indices. These index strings are called keys. As an example, suppose you want to be able to look up each user’s home directory. You could use a hash with the usernames as the keys. The following example creates a hash with two entries-one for user kcb, and one for mgibson:

 my %homedirs = ("kcb", "/home/kbc", "mgibson", "/home/mgibson") ;

As you can see, hashes look a bit like arrays. A hash is a list in which the keys alternate with the corresponding values. Hash variable names start with a %. Adding values to a hash is similar to adding elements to an array:

 $homedirs{"johng"} = "/home/johng";

Note that hashes use curly braces ({}) instead of square brackets ([]) for arguments. You can look up values in a hash by using the key as an index:

 my $homedir = $homedirs{"johng"};               # the home directory for user johng

Here’s a longer example of a hash:

 my %dayabbr = (    "Sunday", "Sun",    "Monday", "Mon"    "Tuesday", "Tues"    "Wednesday", "Wed",    "Thursday", "Thurs"    "Friday", "Fri"    "Saturday", "Sat" ) ; print "The abbreviation for Tuesday is $dayabbr{"Tuesday"}\n";

This hash links the days of the week to their abbreviations.

Note that since the keys are used to look up values, each key must be unique. (The values can be duplicated.)

If you have never used associative arrays, then hashes may seem strange at first. But they are remarkably useful, especially for working with text. We will see examples of how convenient hashes can be later in this chapter, when we have discussed foreach loops and a few other language features.

Working with Hashes

The reverse function swaps the keys of a hash with the values. In this example, abbrdays is a reverse of the hash dayabbr. It translates abbreviations into the full names for the days of the week:

 my %abbrdays = reverse (%dayabbr) ;

Not all hashes reverse well. If a hash contains some duplicate values, when it is reversed it will have some duplicate keys. But duplicate keys are not allowed, so the "extras" are removed. For example, if you reverse the following hash,

 my %roles = (    "McKellen", "Hamlet",    "Jacobi", "Hamlet",    "Stewart", "Claudius", ) ; my %actors = reverse($roles) ;

the new hash, %actors, will contain only two elements, one with the key "Hamlet" and the other "Claudius". It can be difficult to predict which entry Perl will remove, so you should be careful when reversing a hash that might have duplicate values.

The function keys returns a list (in no particular order) of the keys for a hash, as in

 my @fullnames = keys (%dayabbr) ;               # "Sunday", "Monday", etc

Similarly, values returns a list of the values in the hash. For example,

 my @shortnames = values (%dayabbr) ;            # "Sun", "Mon", etc

The list may include duplicate values, if the hash contains two or more keys that have the same value.

The delete function removes a key (and the associated value):

 delete $dayabbr{"Wednesday"};

delete also returns the value it removes, so you could write

 print "Enter a day to delete.\n"; chomp(my $deleteday = <STDIN>);           # must remember to chomp here print "Deleting the pair $deleteday, " . delete $dayabbr{$deleteday} . "\n";




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net