Answers to Chapter 5 Exercises
- Here's one way to do it:
my %last_name = qw{ fred flintstone barney rubble wilma flintstone }; print "Please enter a first name: "; chomp(my $name = <STDIN>); print "That's $name $last_name{$name}.\n";In this one, we used a
qw//list (with curly braces as the delimiter) to initialize the hash. That's fine for this simple data set, and it's easy to maintain because each data item is a simple given name and simple family name, with nothing tricky. But if your data might contain spaces -- for example, ifrobertde niroormary kay placewere to visit Bedrock -- this simple method wouldn't work so well.You might have chosen to assign each key/value pair separately, something like this:
my %last_name; $last_name{"fred"} = "flintstone"; $last_name{"barney"} = "rubble"; $last_name{"wilma"} = "flintstone";Note that (if you chose to declare the hash with
my, perhaps becauseusestrictwas in effect), you must declare the hash before assigning any elements. You can't usemyon only part of a variable, like this:my $last_name{"fred"} = "flintstone"; # Oops!The
myoperator works only with entire variables, never with just one element of an array or hash. Speaking of lexical variables, you may have noticed that the lexical variable$nameis being declared inside of thechompfunction call; it is fairly common to declare eachmyvariable as it is needed, like this.This is another case where
chompis vital. If someone enters the five-character string"fred\n"and we fail tochompit, we'll be looking for"fred\n"as an element of the hash -- and it's not there. Of course,chompalone won't make this bulletproof; if someone enters"fred \n"(with a trailing space), we don't have a way with what we've seen so far to tell that they meantfred.If you added a check whether the given key
existsin the hash, so that you'll give the user an explanatory message when they misspell a name, give yourself extra points for that. - Here's one way to do it:
my(@words, %count, $word); # (optionally) declare our variables chomp(@words = <STDIN>); foreach $word (@words) { $count{$word} += 1; # or $count{$word} = $count{$word} + 1; } foreach $word (keys %count) { # or sort keys %count print "$word was seen $count{$word} times.\n"; }In this one, we declared all of the variables at the top. People who come to Perl from a background in languages like Pascal (where variables are always declared "at the top") may find that way more familiar than declaring variables as they are needed. Of course, we're declaring these because we're pretending that
use strictmay be in effect; by default, Perl won't require such declarations.Next, we use the line-input operator,
<STDIN>, in a list context to read all of the input lines into@words, and then wechompthose all at once. So@wordsis our list of words from the input (if the words were all on separate lines, as they should have been, of course).Now, the first foreach loop goes through all of the words. That loop contains the most important statement of the entire program, the statement that says to add one to
$count{$word}, and put the result back into$count{$word}. Although you could write it either the short way (with the+=operator) or the long way, the short way is just a little bit more efficient, since Perl has to look up$wordin the hash just once.[384][384]Also, at least in some versions of Perl, the shorter way will avoid a warning about using an undefined value that may crop up with the longer one. The warning may also be avoided by using the
++operator to increment the variable, although we haven't shown you that operator yet.For each word in the first
foreachloop, we add one to$count{$word}. So, if the first word isfred, we add one to$count{"fred"}. Of course, since this is the first time we've seen$count{"fred"}, it'sundef. But since we're treating it as a number (with the numeric+=operator, or with+, if you wrote it the long way), Perl convertsundefto for us, automatically. The total is , which is then stored back into$count{"fred"}.The next time through that
foreachloop, let's say the word isbarney. So, we add one to$count{"barney"}, bumping it up fromundefto as well.Now let's say the next word is
fredagain. When we add one to$count{"fred"}, which is already , we get . This goes back into$count{"fred"}, meaning that we've now seenfredtwice.When we finish the first
foreachloop, then, we've counted how many times each word has appeared. The hash has a key for each (unique) word from the input, and the corresponding value is the number of times that word appeared.So now, the second
foreachloop goes through the keys of the hash, which are the unique words from the input. In this loop, we'll see each different word once. For each one, it says something like "fred was seen 3 times."If you want the extra credit on this problem, you could put
sortbeforekeysto print out the keys in order. If there will be more than a dozen items in an output list, it's generally a good idea for them to be sorted, so that a human being who is trying to debug the program will fairly quickly be able to find the item he or she wants.