如何在Perl中打破内部循环的外部循环? [英] How do I break an outer loop from an inner one in Perl?
本文介绍了如何在Perl中打破内部循环的外部循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一段Perl代码,例如:
Suppose I have a piece of Perl code like:
foreach my $x (@x) {
foreach my $y (@z) {
foreach my $z (@z) {
if (something()) {
# I want to break free!
}
# do stuff
}
# do stuff
}
# do stuff
}
如果something()
为true,我想中断(最后一次")所有循环.
If something()
is true, I would like to break ('last') all the loops.
我该怎么做?
我想到了两个选项,但我都不喜欢:
使用GOTO
添加一个将标记something()
为true的布尔变量,请在每个循环恢复之前检查此var,如果last()
为true,则进行检查.
how can I do that?
I thought of two options, both of which I don't like:
Using something GOTO
Adding a boolean variable which will mark something()
is true, check this var in each of the loops before they resume and last()
if it's true.
有什么建议或想法吗?
谢谢.
推荐答案
使用标签:
OUTER:
foreach my $x (@x) {
foreach my $y (@z) {
foreach my $z (@z) {
if (something()) {
last OUTER;
}
# do stuff
}
# do stuff
}
# do stuff
}
这篇关于如何在Perl中打破内部循环的外部循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文