Typical Use of a Hash

5.4 Typical Use of a Hash

At this point, you may find it helpful to see a more concrete example.

The Bedrock library uses a Perl program in which a hash keeps track of how many books each person has checked out, among other information:

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

It's easy to see whether 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 aren't true:

$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 (that is, for each library patron), there's a value that is either a number of books checked out, or undef if that person's library card has never been used.

5.4.1 The exists Function

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

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.

5.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

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

5.4.3 Hash Element Interpolation

You can interpolate a single hash element into a double-quoted string just as you'd expect:

foreach $person (sort keys %books) {  # for each library 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 just the six chararcters of (literally) %books.[16] So we've seen all of the magical characters that need backslashing in double quotes: $ and @, because they introduce a variable to be interpolated; ", since that's the quoting character that would otherwise end the double-quoted string; and \, the backslash itself. Any other characters in a double-quoted string are non-magical and should simply stand for themselves.[17]

[16] Well, it couldn't really 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 we'll see in Chapter 6, the percent sign is frequently used in printf format strings; giving it another meaning here would be terribly inconvenient.

[17] But do 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: 2001
Pages: 205

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