9

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

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
itil memek cantik
  • 1,167
  • 2
  • 11
  • I haven't yet looked at your link, but have you entered `last` into the docs.raku.org website? If so, did what you found help? If not, have you read the **Quicker answers** section of [my answer to your last Q](https://stackoverflow.com/a/70942880/1077672)? If not, please consider doing so. ;) – raiph Feb 03 '22 at 13:39
  • 2
    FYI, and for later readers, especially those who read the PM thread... You've accepted Liz's answer, but it's about `LAST`, which is a phaser that fires when a loop is *left* for the last time, not when it is *entered* for the last time. Fwiw my understanding of the `ref` code in the PM thread is that they presume a loop is iterating a known number of times. So they check the iteration count at the *entry* of the loop code each time around the loop. In Raku, consider using lazy list processing and/or the `ENTER` phaser to nicely address this. (And, one day, macros.) – raiph Feb 03 '22 at 19:28

1 Answers1

10

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
Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105