曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3554|回复: 0
打印 上一主题 下一主题

redo 操作符

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2011-10-9 21:34:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
redo 操作符也用于控制循环,它不经过任何条件测试直接返回到本次循环的顶端,这种概念在 C 语言里并不存在。它和 next 的区别是,next 会正常继续下一次迭代,而 redo 则会重新执行这次迭代。比较下面的程序:
[Perl] 纯文本查看 复制代码
#!/usr/bin/perl

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

运行输出:
# ./redo2.pl
Iteration number 1.

Please choose: last, next, redo, or none of the above? next

Iteration number 2.

Please choose: last, next, redo, or none of the above? next

Iteration number 3.

Please choose: last, next, redo, or none of the above? redo

Iteration number 3.

Please choose: last, next, redo, or none of the above? next

Iteration number 4.

Please choose: last, next, redo, or none of the above? ^C
由输出可见,在使用 redo 时,foreach 停止向前推进,而是在当前所在地重新开始。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-3 11:16 , Processed in 0.089580 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表