I want to cycle through 12 strings, and list every combination of them, for example I have shorted the working code for 3 terms:
foreach my $t1 ('a', 0) {
foreach my $t2 ('b', 0) {
foreach my $t3 ('c', 0) {
my @terms = grep {$_ ne '0'} ($t1, $t2, $t3);
say join ('+', @terms);
}
}
}
which outputs
a+b+c
a+b
a+c
a
b+c
b
c
This is the correct output.
However, I realize that deep nesting of foreach loops isn't good coding practice.
I have looked at alternatives to this in Alternate looping nested foreach loops but this requires this is also really ugly:
my @t1 = ("a", (0) x 132);
my @t2 = ("b", (0) x 132);
my @t3 = ("c", (0) x 132);
my @t4 = ("d", (0) x 132);
my @t5 = ("e", (0) x 132);
my @t6 = ("f", (0) x 132);
my @t7 = ("g", (0) x 132);
my @t8 = ("h", (0) x 132);
my @t9 = ("i", (0) x 132);
my @t10 = ("j", (0) x 132);
my @t11 = ("k", (0) x 132);
my @t12 = ("l", (0) x 132);
my $it = each_array(@t1, @t2, @t3, @t4, @t5, @t6, @t7, @t8, @t9, @t10, @t11, @t12);
while (my ($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $t12) = $it->()) {
my @terms = grep {$_ ne '0'} ($t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $t12);
say join ('+', @terms);
}
this just outputs blank spaces, and doesn't seem to do what I think each_array is supposed to do. I'm not even sure that there will be 132 iterations.
How can I go through these 12 terms without deep nesting of foreach loops?