“最后”的替代在 do 循环中
根据 for last
的 perl 手册 (http://perldoc.perl .org/functions/last.html),last
不能用于打破 do {}
循环,但它没有提到选择。我正在维护的脚本具有以下结构:
do {
...
if (...)
{
...
last;
}
} while (...);
我很确定他想要转到循环的末尾,但它实际上退出了当前的子例程,所以我需要更改 last
或者如果有人可以推荐更好的方法,则重构整个循环。
According to the perl manual for for last
(http://perldoc.perl.org/functions/last.html), last
can't be used to break out of do {}
loops, but it doesn't mention an alternative. The script I'm maintaining has this structure:
do {
...
if (...)
{
...
last;
}
} while (...);
and I'm pretty sure he wants to go to the end of the loop, but its actually exiting the current subroutine, so I need to either change the last
or refactor the whole loop if there is a better way that someone can recommend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将 do“循环”包装在一个裸块中(这是一个循环):
这适用于最后一个和重做,但不适用于下一个;为此,将裸块放置在 do 块内:
Wrap the do "loop" in a bare block (which is a loop):
This works for last and redo, but not next; for that place the bare block inside the do block:
do BLOCK while (EXPR)
很有趣,因为 do 并不是真正的循环结构。因此,last、next 和 redo 不应该在那里使用。当发现这种情况时,去掉最后一个并调整 EXPR 以评估错误。另外,打开严格,这至少应该给你一个警告。
do BLOCK while (EXPR)
is funny in that do is not really a loop structure. So, last, next, and redo are not supposed to be used there. Get rid of the last and adjust the EXPR to evaluate false when that situation is found.Also, turn on strict, which should give you at least a warning here.
从不喜欢 Perl 中的 do/while 循环。
do
并不是真正的循环,这就是为什么last
不会跳出循环。在我们古老的Pascal
茫然中,你无法在中间退出循环,因为根据圣人 Niklaus“一个入口/一个出口”Wirth 的说法,这是错误的。因此,我们必须创建一个退出标志。在 Perl 中,它看起来像这样:现在,你可以看到 Pascal 从未流行起来。
Never a fan of do/while loops in Perl. the
do
isn't really a loop which is whylast
won't break out of it. In our oldPascal
daze you couldn't exit a loop in the middle because that would be wrong according to the sage Niklaus "One entrance/one exit" Wirth. Therefore, we had to create an exit flag. In Perl it'd look something like this:Now, you can see while Pascal never caught on.
为什么不直接使用 while 循环呢?
您可能需要稍微改变您的逻辑,以适应您的测试是在循环的开始而不是结束的事实,但这应该是微不足道的。
顺便说一句,如果您使用 Delphi,您实际上可以摆脱 Pascal 循环,并且 Delphi 确实流行了一段时间,直到 Microsoft 醒悟并推出了 .net 语言。
Why not just use a while loop?
You might have to change your logic slightly to accommodate the fact that your test is at the beginning instead of end of your loop, but that should be trivial.
By the way, you actually CAN break out of a Pascal loop if you're using Delphi, and Delphi DID catch on for a little while until Microsoft wised up and came out with the .net languages.
因此,在“while()”中使用布尔值并将其设置在“last”的位置...
So, use a boolean in the 'while()' and set it where you have 'last'...
聚会迟到了——我最近一直在搞乱
for(;;)
。在我的基本测试中,对于条件表达式A
和B
,您想要执行的操作:可以完成为:
有点难看,但也许并不比其他表达式更难看解决方法:)。示例:
输出
1 2 3
。如果需要,您可以组合增量:(使用
||
因为它的优先级高于,
)Late to the party - I've been messing with
for(;;)
recently. In my rudimentary testing, for conditional expressionsA
andB
, what you want to do with:can be accomplished as:
A bit ugly, but perhaps not more so than the other workarounds :) . An example:
Outputs
1 2 3
. And you can combine the increment if you want:(using
||
because it has higher precedence than,
)