“最后”的替代在 do 循环中

发布于 2024-10-06 07:13:08 字数 429 浏览 3 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

季末如歌 2024-10-13 07:13:08

将 do“循环”包装在一个裸块中(这是一个循环):

{
    do {
        ...
        if (...) 
        {
            ...
            last;
        }
    } while (...);
}

这适用于最后一个和重做,但不适用于下一个;为此,将裸块放置在 do 块内:

do {{
    ...
    if (...) 
    {
        ...
        next;
    }
    ...
}} while (...);

Wrap the do "loop" in a bare block (which is a loop):

{
    do {
        ...
        if (...) 
        {
            ...
            last;
        }
    } while (...);
}

This works for last and redo, but not next; for that place the bare block inside the do block:

do {{
    ...
    if (...) 
    {
        ...
        next;
    }
    ...
}} while (...);
花海 2024-10-13 07:13:08

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.

飘逸的'云 2024-10-13 07:13:08

从不喜欢 Perl 中的 do/while 循环。 do 并不是真正的循环,这就是为什么 last 不会跳出循环。在我们古老的Pascal茫然中,你无法在中间退出循环,因为根据圣人 Niklaus“一个入口/一个出口”Wirth 的说法,这是错误的。因此,我们必须创建一个退出标志。在 Perl 中,它看起来像这样:

my $endFlag = 0;
do {
    ...
    if (...) 
    {
        ...
        $endFlag = 1;
    }
} while ((...) and (not $endFlag));

现在,你可以看到 Pascal 从未流行起来。

Never a fan of do/while loops in Perl. the do isn't really a loop which is why last won't break out of it. In our old Pascal 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:

my $endFlag = 0;
do {
    ...
    if (...) 
    {
        ...
        $endFlag = 1;
    }
} while ((...) and (not $endFlag));

Now, you can see while Pascal never caught on.

猫卆 2024-10-13 07:13:08

为什么不直接使用 while 循环呢?

while (...) {
  ...
  if (...) {
    last;
  }
}

您可能需要稍微改变您的逻辑,以适应您的测试是在循环的开始而不是结束的事实,但这应该是微不足道的。

顺便说一句,如果您使用 Delphi,您实际上可以摆脱 Pascal 循环,并且 Delphi 确实流行了一段时间,直到 Microsoft 醒悟并推出了 .net 语言。

Why not just use a while loop?

while (...) {
  ...
  if (...) {
    last;
  }
}

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.

滴情不沾 2024-10-13 07:13:08

@“http://perldoc.perl.org/functions/last.html”:
last 不能用于退出返回值(例如 eval {} 、 sub {} 或 do {} )的块,也不应用于退出 grep() 或 map() 操作。

因此,在“while()”中使用布尔值并将其设置在“last”的位置...

@ "http://perldoc.perl.org/functions/last.html":
last cannot be used to exit a block that returns a value such as eval {} , sub {} or do {} , and should not be used to exit a grep() or map() operation.

So, use a boolean in the 'while()' and set it where you have 'last'...

平定天下 2024-10-13 07:13:08

聚会迟到了——我最近一直在搞乱for(;;)。在我的基本测试中,对于条件表达式 AB,您想要执行的操作:

do {
    last if A;
} while(B);

可以完成为:

for(;; B || last) {
    last if A;
}

有点难看,但也许并不比其他表达式更难看解决方法:)。示例:

my $i=1; 
for(;; $i<=3 || last) { 
    print "$i ";
    ++$i;
}

输出 1 2 3。如果需要,您可以组合增量:(

my $i=1;
for(;; ++$i, $i<=3 || last) { 
    print "$i ";
}

使用 || 因为它的优先级高于 ,

Late to the party - I've been messing with for(;;) recently. In my rudimentary testing, for conditional expressions A and B, what you want to do with:

do {
    last if A;
} while(B);

can be accomplished as:

for(;; B || last) {
    last if A;
}

A bit ugly, but perhaps not more so than the other workarounds :) . An example:

my $i=1; 
for(;; $i<=3 || last) { 
    print "$i ";
    ++$i;
}

Outputs 1 2 3. And you can combine the increment if you want:

my $i=1;
for(;; ++$i, $i<=3 || last) { 
    print "$i ";
}

(using || because it has higher precedence than ,)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文