Section 6.4. Typical Use of a Hash

6.4. Typical Use of a Hash

At this point, a concrete example might help.

The Bedrock library uses a Perl program in which a hash tracks how many books each person has checked out:

     $books{"fred"} = 3;     $books{"wilma"} = 1;

It's easy to see if an element of the hash is true or false; do this:

     if ($books{$someone}) {       print "$someone has at least one book checked out.\n";     }

But there are some elements of the hash that are false:

     $books{"barney"} = 0;       # no books currently checked out     $books{"pebbles"} = undef;  # no books EVER checked out - a new library card

Since Pebbles has never checked out any books, her entry has the value of undef, rather than 0.

There's a key in the hash for everyone who has a library card. For each key (or library patron), the value is the number of books checked out or undef if that person's library card has never been used.

6.4.1. The exists Function

To see if a key exists in the hash, (whether someone has a library card), use the exists function, which returns a true value if the given key exists in the hash, whether the corresponding value is true or false:

     if (exists $books{"dino"}) {       print "Hey, there's a library card for dino!\n";     }

That is to say, exists $books{"dino"} will return a true value if (and only if) dino is found in the list of keys from keys %books.

6.4.2. The delete Function

The delete function removes the given key (and its corresponding value) from the hash. (If there's no such key, its work is done; there's no warning or error in that case.)

     my $person = "betty";     delete $books{$person};  # Revoke the library card for $person

This is not the same as storing undef into that hash element. Checking exists($books{"betty"}) will give opposite results in these two cases. After a delete, the key can't exist in the hash; after storing undef, the key must exist.

In the example, delete versus storing undef is the difference between taking away Betty's library card versus giving her a card that has never been used.

6.4.3. Hash Element Interpolation

You can interpolate a single hash element into a double-quoted string:

     foreach $person (sort keys %books) {               # each patron, in order       if ($books{$person}) {         print "$person has $books{$person} items\n";   # fred has 3 items       }     }

But there's no support for entire hash interpolation; "%books" is nothing more than the literal six characters of %books.[*] You've seen all of the magical characters that need backslashing in double quotes $ and @ introduce a variable that Perl will try to interpolate, ", since that's the quoting character that would otherwise end the double-quoted string and \, the backslash. Any other characters in a double-quoted string are nonmagical and should stand for themselves.[*]

[*] Well, it couldn't be anything else. If we tried to print out the entire hash as a series of key/value pairs, that would be nearly useless. And, as you saw in the last chapter, the percent sign is frequently used in printf format strings. Giving it another meaning here would be terribly inconvenient.

[*] Beware of the apostrophe ('), left square bracket ([), left curly brace ({), the small arrow (->), or double-colon (::) following a variable name in a double-quoted string, as they could perhaps mean something you didn't intend.

Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: