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 tutorials each person has checked out, among other information:

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

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

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

But there are some elements of the hash that aren't true:

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

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

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 tutorials checked out, or undef if that person's library card has never been used.

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 $tutorials{"dino"}) {
 print "Hey, there's a library card for dino!\n";
}

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

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 $tutorials{$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($tutorials{"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.

Hash Element Interpolation

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

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

But there's no support for entire hash interpolation; "%tutorials" is just the six chararcters of (literally) %tutorials.[136] 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.[137]

[136]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 "I/O Basics", the percent sign is frequently used in printf format strings; giving it another meaning here would be terribly inconvenient.

[137]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.