in reply to subroutine to calculate multiplication tables
Hi, read this
Tutorials: Debugging and Optimization: Basic debugging checklist
Armed with that knowledge, I modify your program to
use strict; use warnings; use diagnostics; use 5.01000; &table( 2, 3, 4 ); sub table { my $i = 1; my $loop; foreach $loop (@_) { warn " loop $loop \n"; for ( $i ; $i <= 10 ; $i++ ) { my $ans = $i * $loop; print "$ans "; } print "\n"; } } __END__
And the output is
loop 2 2 4 6 8 10 12 14 16 18 20 loop 3 loop 4
So, what is happening is, it is looping over the arguments to the subroutine correctly, but the problem of printing the values, the problem with the inner loop is, the condition is false for some reason
To see why the warn is now
and the outputwarn " loop $loop i $i \n";
loop 2 i 1 2 4 6 8 10 12 14 16 18 20 loop 3 i 11 loop 4 i 11
So for ( $i ; $i <= 10 ; $i++ ) { first executes $i; , executed once, on the first run through the loop (loop initialization). It generates this warning Useless use of private variable in void context
Then second part $i <= 10; checks if 11 is greater than 10, so the loop doesn't execute
Why? Because of scoping, read this
Tutorials: Variable Scoping in Perl: the basics, Coping with Scoping
If you move my $i = 1; into the inner for loop like for ( my $i = 1; $i <= 10 ; $i++ ) { it will work as you want
2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40
They call this c-style for loop. The perl-style for loop would be for my $i ( 1 .. 10 ) {
For learning this level of perlintro I liked http://learn.perl.org/books/beginning-perl/ book, its available as free pdf
Hi, this being your 8th post and all, I've got a link for you :)
PerlMonks FAQ: How do I compose an effective node title?
I recognize that you're very new so it can be real hard to compose a title, but instead of "help me" you might include words you used in the question, words like: loop, sub, parameter, print
|
|---|