Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Subroutine & For loop

by Anonymous Monk
on Jul 25, 2014 at 10:08 UTC ( [id://1095017]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I would like to call a subroutine repeatedly based on a condition from a for loop and each time pass different parameters. My code is calling the subroutine only the first time. I am not sure which is the correct format to call a subroutine from a for loop. Please help. Thank you!

Replies are listed 'Best First'.
Re: Subroutine & For loop
by roboticus (Chancellor) on Jul 25, 2014 at 14:26 UTC

    Here's a trivial example:

    sub doit { my ($arg1, $arg2) = @_; print "$arg1 squared is $arg2\n"; } for (0 .. 10) { doit( $_, $_*$_); }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Subroutine & For loop
by LanX (Saint) on Jul 25, 2014 at 10:16 UTC
    To be able to help you, you need to show us first what you tried.

    Please put it within code tags...

    Cheers Rolf

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

Re: Subroutine & For loop
by Anonymous Monk on Jul 25, 2014 at 10:11 UTC
Re: Subroutine & For loop
by Laurent_R (Canon) on Jul 25, 2014 at 18:58 UTC
    A oneliner example:
    $ perl -e 'for my $c (1..10) {print_square($c)}; sub print_square{my $ +d = shift; print $d*$d, "\n";}' 1 4 9 16 25 36 49 64 81 100
Re: Subroutine & For loop
by tbone654 (Beadle) on Jul 25, 2014 at 14:58 UTC
    see module switch::plain , if I understand the question properly it should help...
      > if I understand the question properly

      Don't feed the cryptic! ;-)

      Cheers Rolf

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095017]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found