| Previous | Table of Contents | Next |
Instead of always beginning each loop at the top and mindlessly proceeding to the bottom, you can jump around a bit with next, last, and redo (Figure 1-10).
Figure 1-10 next, last, and redo
Suppose a 12-year-old runs one of the tickets programs. Children under 17 arent allowed to see R-rated movies without a parent, and this little tyke doesnt have a parent handy. Of course, because you want your computer program to enforce the law dutifully, it should skip over the R-rated movies.
Well demonstrate next, last, and redo with two programs. tickets5 uses next and last, and lotto uses redo.
tickets5, shown in Listing 1-16, immediately moves to the next movie if the current movie is R rated and declares the current iteration to be the last if the user says yes to a movie.
Listing 1-16 tickets5: Using next and last to control loop flow
#!/usr/bin/perl -w
@movies = (Star Wars, Porkys, Rocky 5, Terminator 2,
The Muppet Movie);
@ratings = (PG, R, PG-13, R, G); # The ratings for @movies.
print Welcome to Perlplex Cinema, little tyke! \n;
for ($i = 0; $i <= 4; $i++) { # Count from 0 to 4
next if $ratings[$i] eq R; # Skip over R-rated movies
print Would you like to see $movies[$i]? ;
chomp($answer = <>);
last if $answer eq yes; # Exit the loop, if the
# child agrees to a movie
}
if ($answer eq yes) { print Thatll be \$4.25\n }
else { print Im sorry you dont see anything you like.\n }
The next statement checks the rating of the $movieth title in @movies. The condition ($ratings[$i] eq R) triggers on Porkys and Terminator 2, so those movies wont be mentioned to the child. If the child says yes to any of the movies, the last statement triggers and the loop immediately exits, at which time the user is asked for money.
% tickets5
RESULT: Welcome to Perlplex Cinema, little tyke!
Would you like to see Star Wars? no
(so the next statement skips over Porkys)
Would you like to see Rocky 5? yes
(and the last statement exits the loop.)
Thatll be $4.25.
(Youth discount)
Suppose you want to begin another loop iteration, but without evaluating the stop condition or the increment step. The redo statement does just that, as in lotto, which appears in Listing 1-17.
Listing 1-17 lotto: Using redo to begin another iteration
#!/usr/bin/perl -w
print ** Pick 4 LottoMatic ** \n;
print ** Type four digits (0 through 9) ** \n\n;
for ($i = 1; $i <= 4; $i++) {
print Choice $i: ;
chomp($digit[$i] = <>);
redo if $digit[$i] > 9;
redo if $digit[$i] < 0;
}
print Your choices: @digit \n;
lotto wants users to enter four digits, one at a time. If the user enters a number that isnt between 0 and 9, one of the redo statements triggers, sending control back to just past the top of the loop so that $i is neither incremented nor compared to 4. Its a way to say, Lets try that again.
% lotto
RESULT: ** Pick 4 LottoMatic **
** Type four digits (0 through 9) **
Choice 1: 7
Choice 2: 12
(The first redo triggers.)
Choice 2: 1 Choice 3: 8 Choice 4: -3
(The second redo triggers.)
Choice 4: 6 Your choices: 7 1 8 6
A confession: for and foreach are actually just different names for the same thing. You can loop through arrays with for, and you can specify a start, stop, and increment with foreach. So why are there two names for the same statement? Because C programmers like their for loops and shell programmers like their foreach loops. Even though youve been let in on this little secret (few Perl programmers know this!), you shouldnt take advantage of it because other programmers will think of C when they see for and think of shells when they see foreach. This book pretends theyre distinct, but now you know better.
| Previous | Table of Contents | Next |