To determine whether the current loop iteration is the last one or not

Viewed 97

How do we know and determine whether or not the current point of loop is the last one (as it's simply done in Perl due to having \ ref. operator see on) ?

The edited corrected link: https://perlmonks.org/?node_id=11140741

Please help point a workaround. Thanks

1 Answers

If you want to do something if it is the last iteration of the loop, why not use the LAST phaser?

for ^5 {
    FIRST say "$_ is the first";
    LAST say "$_ was the last";
    .say;
}

which outputs:

0 is the first
0
1
2
3
4
4 was the last
Related