Chinaunix首页 | 论坛 | 博客
  • 博客访问: 855426
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-11 09:36:49

last: the same to break in C. will jump out of the loop.
  1. # Print all input lines mentioning fred, until the __END__ marker
  2. while (<STDIN>) {
  3.   if (/__END__/) {
  4.     # No more input on or after this marker line
  5.     last;
  6.   } elsif (/fred/) {
  7.     print;
  8.   }
  9. }
  10. ## last comes here #
next: begin a new loop without execute the remained statements. Sometimes you’re not ready for the loop to finish, but you’re done with the current
iteration. That’s what the next operator is good for. It jumps to the inside of the bottom
of the current loop block.‡ After next, control continues with the next iteration of the
loop (much like the “continue” operator in C or a similar language):
  1. while (<>) {
  2.   foreach (split) { # break $_ into words, assign each to $_ in turn
  3.     $total++;
  4.     next if /\W/; # strange words skip the remainder of the loop
  5.     $valid++;
  6.     $count{$_}++; # count each separate word
  7.     ## next comes here ##
  8.   }
  9. }
  10. print "total things = $total, valid words = $valid\n";
  11. foreach $word (sort keys %count) {
  12.   print "$word was seen $count{$word} times.\n";
  13. }

redo:It says to go back to the top of the
current loop block, without testing any conditional expression or advancing to the next
iteration.
  1. # Typing test
  2. my @words = qw{ fred barney pebbles dino wilma betty };
  3. my $errors = 0;
  4. foreach (@words) {
  5.   ## redo comes here ##
  6.   print "Type the word '$_': ";
  7.   chomp(my $try = <STDIN>);
  8.   if ($try ne $_) {
  9.     print "Sorry - That's not right.\n\n";
  10.     $errors++;
  11.     redo; # jump back up to the top of the loop
  12.   }
  13. }
  14. print "You've completed the test, with $errors errors.\n";

  1. foreach (1..10) {
  2.   print "Iteration number $_.\n\n";
  3.   print "Please choose: last, next, redo, or none of the above? ";
  4.   chomp(my $choice = <STDIN>);
  5.   print "\n";
  6.   last if $choice =~ /last/i;
  7.   next if $choice =~ /next/i;
  8.   redo if $choice =~ /redo/i;
  9.   print "That wasn't any of the choices... onward!\n\n";
  10. }
  11. print "That's all, folks!\n";




阅读(302) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~