Section A.5.  Answers to Chapter 6 Exercises

Table of Contents

A.5. Answers to Chapter 6 Exercises

  1. 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. If your data might contain spacesfor example, if robert de niro or mary kay place were to visit Bedrockthis method wouldn't work as 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 because use strict was in effect) you must declare the hash before assigning any elements. You can't use my on only part of a variable, like this:

        my $last_name{"fred"} = "flintstone";  # Oops!

    The my operator works only with entire variables and never with one element of an array or hash. Speaking of lexical variables, you may have noticed the lexical variable $name is being declared inside of the chomp function call; it is fairly common to declare each my variable as it is needed, like this.

    This is another case where chomp is vital. If someone enters the five-character string "fred\n" and we fail to chomp it, we'll be looking for "fred\n" as an element of the hashand it's not there. chomp alone 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 meant fred.

    If you added a check whether the given key exists in the hash, so you'll give the user an explanatory message when he misspells a name, give yourself extra points for that.

  2. 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 declared at the top) may find that way more familiar than declaring variables as they are needed. We're declaring these because we're pretending that use strict may 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 we chomp those all at once. So, @words is our list of words from the input if the words were all on separate lines as they should have been.

    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}. Though you could write it the short way (with the += operator) or the long way, the short way is a little bit more efficient since Perl has to look up $word in the hash only once.[*] For each word in the first foreach loop, we add one to $count{$word}. If the first word is fred, we add one to $count{"fred"}. Since this is the first time we've seen $count{"fred"}, it's undef. We're treating it as a number (with the numeric += operator, or with +, if you wrote it the long way), so Perl converts undef to 0 for us, automatically. The total is 1, which is stored back into $count{"fred"}.

    [*] In some versions of Perl, the shorter way will avoid a warning about using an undefined value that may appear with the longer one. The warning can also be avoided by using the ++ operator to increment the variable, though we haven't shown you that operator yet.

    The next time through that foreach loop, let's say the word is barney. We add one to $count{"barney"}, bumping it up from undef to 1, as well.

    Suppose the next word is fred again. When we add one to $count{"fred"}, which is 1, we get 2. This goes back into $count{"fred"}, meaning that we've seen fred twice.

    When we finish the first foreach loop, 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.

    The second foreach loop 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 sort before keys to 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 to sort them, so that a human being who is trying to debug the program will fairly quickly be able to find the item she wants.

    Table of Contents
    © 2000- NIV