Hello Monks. I've managed to get the following modular exercise to work, which looks at a range of integers, and provides the total of those which are not divisible by 13 or 17.
#!/usr/bin/env perl use warnings; use strict; use feature ':5.10'; use File::Slurp; my @lines = (1..2310) ; my @s = (13,17); my @list1 = (0..$s[0]-1); my @list2 = (0..$s[1]-1); foreach $a (@list1) { my @remain1 = grep { $_% $s[0] ne $a} @lines; foreach $b (@list2) { my @remain2 = grep { $_% $s[1] ne $b} @remain1; say $s[0], " ", $a, " ",$s[1]," ", $b, " ", scalar @remain2; } }
# but the text below which plans to add a further prime number to the integer sequence and puts in a further nested foreach loop, does not work. It gives messages that $c is not defined.
#!/usr/bin/env perl use warnings; use strict; use feature ':5.10'; use File::Slurp; my @lines = (1..2310); my @s = (13,17,19); my @list1 = (0..$s[0]-1); my @list2 = (0..$s[1]-1); my @list3 = (0..$s[2]-1); foreach $a (@list1) { my @remain1 = grep { $_% $s[0] ne $a} @lines; foreach $b (@list2) { my @remain2 = grep { $_% $s[1] ne $b} @remain1; foreach $c (@list3) { my @remain3 = grep { $_% $s[2] ne $c} @remain2; say $s[0], " ", $a, " ",$s[1]," ", $b, " ", $s[2]," ", $c, + " ", scalar @remain3; } } }
#I'm not sure what I am doing wrong here. BTW I am totally new to perl and to programming - this is my very first perl program!
In reply to Nested loops by robert44444uk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |