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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.