fidda has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying out my HW, where I'm asked to practice nested for loops.

Req:

ID: 001 GRP:1 . . ID:020 GRP:20 ID:021 GRP:1 . . ID:120 GRP:20

but I did something wrong.

This is my looping -
for (my $i = 1; $i < 7; $i++) { for (my $g = 1; $g < 21; $g++) { my $j = $i * $g; my $id = sprintf ("%03d", $j); print "ID: $id\t GRP: $g \n"; } }
Please point out what am I doing wrong. TIA

Replies are listed 'Best First'.
Re: Need help with nested for loops
by davido (Cardinal) on Jan 07, 2015 at 20:07 UTC

    I started trying to answer this question a couple of times. The code is pretty straight-forward, and certainly fixing whatever is wrong should be pretty simple.

    But I failed to come up with a solution because I don't know what the problem is. It's not that I don't understand how the code works. It's that I don't understand how it works differently from your needs and expectations, and that is because your needs and expectations haven't been expressed clearly and unambiguously enough.

    Could you please follow-up by posting how the output this produces differs from the output you require? Maybe you could show the first 30 lines of correct output, and the first 30 lines of incorrect output. Or just explain better what you want to see.


    Dave

Re: Need help with nested for loops
by blindluke (Hermit) on Jan 07, 2015 at 20:11 UTC

    You are probably expecting consecutive index numbers, but you are printing the results of the  $i * $g multiplication. During the first outer loop iteration,  $i = 1, $g = 1 .. 20, you will get what you expect. During the second,  $i = 2, and multiplying it by the range 1 .. 20 results in 2, 4, 6, 8, ... instead of the 21, 22, 23 ... range you probably want.

    In simple assignments like this one, it's good to test your loop on paper, until you are sure the method works.

    Also, take a look at the range operator. You can write your loops in a much more readable way:

    for my $i (1..6) { print "$i\n"; }

    Update:

    Take a look at the following example. It's not your exactly your homework assignment, but upon closer inspection you will find that it solves the same problem. Notice the range operator, the modulo operator, and think on why the $grp = 5 if $grp == 0; is needed. Modify the code to suit your needs, and good luck with your next assignments.

    #!/usr/bin/perl use warnings; use strict; for my $id (1 .. 30) { my $grp = $id % 5; $grp = 5 if $grp == 0; printf ("ID: %03d\t GRP: %d \n", $id, $grp); }

    - Luke

Re: Need help with nested for loops
by LanX (Saint) on Jan 07, 2015 at 20:02 UTC
    Not sure what your problem¹ is, but if you want a consecutive count try $j++ .

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    ¹) from How (not) to ask a question Include Sample Data (I/O)

Re: Need help with nested for loops
by GotToBTru (Prior) on Jan 07, 2015 at 20:02 UTC

    What do you expect? What do you get?

    I updated to strike thru my first question, but I might need to put it back in. If I understand your required output, I don't see it as something to do using nested for loops. Can you fill in a few more sample values?

    1 Peter 4:10
Re: Need help with nested for loops
by Anonymous Monk on Jan 07, 2015 at 21:20 UTC
    my $j = $i * $g;
    This is what is wrong (as explained). The nested c-style loops can produce desired output if you do it like this:
    use strict; use warnings; my $group = 20; for ( my $id = 0; $id < 120; $id += $group ) { for ( my $grp = 1; $grp <= $group; $grp += 1 ) { printf "ID: %d\tGRP: %d\n", $id + $grp, $grp; } }