robert44444uk has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nested loops
by nuance (Hermit) on Sep 13, 2015 at 13:42 UTC | |
by robert44444uk (Acolyte) on Sep 13, 2015 at 13:45 UTC | |
by stevieb (Canon) on Sep 13, 2015 at 14:05 UTC | |
by AnomalousMonk (Archbishop) on Sep 13, 2015 at 15:22 UTC | |
|
Re: Nested loops
by Laurent_R (Canon) on Sep 13, 2015 at 15:03 UTC | |
by robert44444uk (Acolyte) on Sep 14, 2015 at 21:42 UTC | |
by shadowsong (Pilgrim) on Sep 14, 2015 at 22:26 UTC | |
by robert44444uk (Acolyte) on Sep 20, 2015 at 18:58 UTC | |
|
Re: Nested loops
by danaj (Friar) on Sep 16, 2015 at 02:58 UTC | |
by robert44444uk (Acolyte) on Sep 20, 2015 at 18:56 UTC |