|
|
| XP is just a number | |
| PerlMonks |
Re: Need help with nested for loopsby blindluke (Hermit) |
| on Jan 07, 2015 at 20:11 UTC ( [id://1112559]=note: print w/replies, xml ) | Need Help?? |
|
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:
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.
- Luke
In Section
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||