Operation on a Series of Integers in Perl 6
I was a bit silent about it but I keep working on the Perl6::Cookbook Here is one of the entries from chapter 2.
Iterating over a series of integers in a range is similar in Perl 6 to the same code in Perl 5 except of slightly different format of the foreach loop that is spelled only as for now.
my $x = 23; my $z = 25; for $x .. $z -> $i { say $i; }
Will print the numbers 23, 24, 25 as expected.
C-style, 3-part for loops are also available in Perl 6 and they are called loop but they are not really recommended. Better to use the for loop as described above. You could say this, but it has the same issues with off-by-one errors as we have already learned to avoid in Perl 5.
loop (my $i = $x; $i <= $z; $i++) { say $i; }
Iterating over every 2nd number is also possible with the for loop of Perl 6 using the by adverb.
for $x .. $y :by(2) -> $i { say $i;
}
Obviously you could use it to jump any, even negative numbers.
Unfortunately as I am writing this, the above code using the by adverb does not yet work in Rakudo.