A Number Speller

This second example isn't particularly useful for the real world, but does show some more complex uses of ifs, whiles, and various tests. This script asks you to enter a single-digit number (and has the usual verification tests to make sure you have indeed entered a single-digit number), and then it spells out that number for you—in other words, 2 is two, 5 is five, and so on. Then, it asks you if you want to enter another number, and if so, it repeats the process. Here's an example:

% numspeller.pl
Enter the number you want to spell: foo
No strings.  0 through 9 please..
Enter the number you want to spell: 45
Too big. 0 through 9 please.
Enter the number you want to spell: 6
Number 6 is six
Try another number (y/n)?: foo
y or n, please
Try another number (y/n)?: y
Enter the number you want to spell: 3
Number 3 is three
Try another number (y/n)?: n
					

Listing 7.2 shows the full code.

Listing 7.2. The numspeller.pl Script
1:  #!/usr/local/bin/perl -w
2:  # numberspeller:  prints out word approximations of numbers
3:  # simple version, only does single-digits
4:
5:  $num = 0;    # raw number
6:  $exit = "";  # whether or not to exit the program.
7:  %numbers = (
8:      1 => 'one',
9:      2 => 'two',
10:     3 => 'three',
11:     4 => 'four',
12:     5 => 'five',
13:     6 => 'six',
14:     7 => 'seven',
15:     8 => 'eight',
16:     9 => 'nine',
17:     0 => 'zero'
18: );
19:
20: while ($exit ne "n") {
21:
22:     while () {
23:         print 'Enter the number you want to spell: ';
24:         chomp($num = <STDIN>);
25:         if ($num !~ /d/ or $num =~ /D/) { # test for strings
26:             print "No strings.  0 through 9 please.
";
27:             next;
28:         }
29:         if ($num > 9) { # numbers w/more than 1 digit
30:             print "Too big. 0 through 9 please.
";
31:             next;
32:         }
33:         if ($num < 0) { # negative numbers
34:             print "No negative numbers.  0 through 9 please.
";
35:             next;
36:         }
37:         last;
38:     }
39:
40:     print "Number $num is ";
41:     print $numbers{$num} ;
42:     print "
";
43:
44:     while () {
45:         print 'Try another number (y/n)?: ';
46:         chomp ($exit = <STDIN>);
47:         $exit = lc $exit;
48:         if ($exit ne 'y' && $exit ne 'n') {
49:             print "y or n, please
";
50:         }
51:         else { last; }
52:     }
53: }
					

whiles within whiles, and ifs after ifs. Let's start from the outer loop, and work inward. The first while loop, in line 20, includes the bulk of the entire script in its block.

That's because the entire script will repeat, or not, based on the yes/no prompt at the end. At the start, however, we need to run it at least once, so the test in line 8 will return true (note that we carefully initialized the $exit variable so that it would).

The second while loop, lines 22 through 38, tests our input through the use of three ifs. The first one checks to make sure you haven't typed any strings using a regular expression; the second one makes sure you haven't typed a number greater than 9 (this version of the script only tests for single-digit numbers); and the third tests for negative numbers. If any of these three tests are true, we use the next keyword, which skips the rest of the block and goes back up to the test for the nearest enclosing loop—which in this case is still that infinite while loop that started in 10. If the input meets all three criteria—it's not a string, and it's in between 0 and 9, then we call last, which breaks out of the nearest while loop and goes onto the next statement in the block.

That next statement is line 40, where we print the introductory message. At the beginning of the script set up a hash with a mapping from all the supported digits to their fully spelled out equivalents. Use a simple hash lookup to retrieve the word that corresponds to the digit entered by the user, and print that out. An ifelsif type of statement could have been used here to test for each of the possible digits the user could enter, but dealing with the hash is easier.

When our number is printed, we can do it all over again if we want, thanks to the final while loop in lines 44 through 52. This one prompts for a y or an n character in response to the “Try another number” prompt. Note the call to the lc function in line 47—if the user types a capital Y or a capital N, we'll still accept that input because the lc function will lowercase it before we actually test. That line saves us some extra tests in the body of the while.

You'll notice that this chunk of code doesn't actually determine what to do if the reply is y or n; all it does is verify that it is indeed y or n. That's because this code doesn't need to do anything. When $exit has the appropriate value, the outer while loop ends, and we return right back up to the top again to that test in the first while. If the reply was n, that test returns false and the script exits. Otherwise, we start the body of that outer while again, and continue until the user gets bored and types n to exit the script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset