Aleksei Averchenko
Aleksei Averchenko

Reputation: 1776

How to control nestedness of loops in Perl 6?

This program should've written triples of indices that have a sum less or equal to 7:

for ((1..7) X (1..7)) X (1..7) {
 .say if [+] $_ <= 7;
}

I thought it would only loop over the top level of the list (and the code would have an error in the loop body then, but it's not the point), but it just loops over individual numbers, which is frustrating :( Is there a neat trick to avoid it? And BTW, is there a way to make an n-ary direct product?

Upvotes: 4

Views: 192

Answers (1)

J-16 SDiZ
J-16 SDiZ

Reputation: 26910

the easiest way to to name the reference

for (1..7) X (1..7) -> $a, $b { }

Upvotes: 4

Related Questions