判断当前循环迭代是否是最后一次

use*_*456 9 raku

我们如何知道并确定循环的当前点是否是最后一个(因为在 Perl 中由于有 \ ref. 运算符可见而简单地完成了这一点)?

编辑后的更正链接: https://perlmonks.org/? node_id=11140741

请帮忙指出解决方法。谢谢

Eli*_*sen 10

如果您想在循环的最后一次迭代时执行某些操作,为什么不使用LAST移相器呢?

for ^5 {
    FIRST say "$_ is the first";
    LAST say "$_ was the last";
    .say;
}
Run Code Online (Sandbox Code Playgroud)

其输出:

0 is the first
0
1
2
3
4
4 was the last
Run Code Online (Sandbox Code Playgroud)

  • [移相器](https://docs.raku.org/language/phasers) (5认同)