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

warn " loop $loop i $i \n";
and the output
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


In reply to Re: Help me by Anonymous Monk
in thread subroutine to calculate multiplication tables by Thakur

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.