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

I need help with problem... i need to create a program that have two subroutines add() and multiply() that will add or multiply its arguments and print the result in screen. this is my current code: if ($ARGV[0] =~ /add/) { $rtn = add($ARGV[1]); print "The sum is: $rtn"; } elsif ($ARGV[0] =~ /multiply/) { $rtn = multiply($ARGV[1]); print "The product is: $rtn"; } sub add { } sub multiply { } can anybody fill me in this code..im kinda confused.. thanks for your help guys..

Replies are listed 'Best First'.
Re: subroutines problem
by mrborisguy (Hermit) on Jul 20, 2005 at 01:01 UTC

    Just a question to clarify: How are you adding just one argument? I mean, your code seems to be implying that you'll be doing something like add( 5 ); - that is if $ARGV[1] contains 5. That just doesn't seem to make sense, most of the time adding takes two parameters, like add( 5, 2 ), I can reasonably guess what that will do, but not add( 5 ).

        -Bryan

      yes Brayan your right, the script would be called with 2 arguments, similar to... add 3 5 which will pass the 'add' argument ($ARGV[0]) and the '3' and '5' arguments ($ARGV[1] and $ARGV[2]).
Re: subroutines problem
by dorko (Prior) on Jul 20, 2005 at 01:24 UTC
    Hi newly, and welcome.

    Firstly, start your code with use strict; and use warnings;. Secondly you might try indenting your code a bit so that it's easier to read. Thirdly, for string comparisons you want to use eq and not =~.

    Lastly, I think mrborisguy is right on the money. You'll have to take three arguments from the command line. (An operation and two numbers.) If your (homework?) problem description says otherwise, that's do-able too.

    use strict; use warnings; if ($ARGV[0] eq "add") { $rtn = add($ARGV[1]); print "The sum is: $rtn"; } elsif ($ARGV[0] eq "multiply") { $rtn = multiply($ARGV[1]); print "The product is: $rtn"; } else { # error message goes here # argv[0] was neither add nor multiply } sub add { } sub multiply { }
    (Note: code still badly broken, but better.)

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: subroutines problem
by NetWallah (Canon) on Jul 20, 2005 at 06:29 UTC
    FYI - here is one way to code an "add" routine - this one allows an unlimited number of scalar arguments to be added together:
    sub add{ shift(@_) + (@_? add(@_):0) } #Test the "add" routine .. print add(qw/3 0 0 1 0 1 01 1 1 1 1 0 1 /); # Outputs : # 11

         "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk

      i got this code but when i tested it i got no result: if ($ARGV[0] eq "add") { $rtn = add($ARGV[1]); print " The sum is $rtn"; } elsif ($ARGV[0] eq "multiply") { $rtn = multiply($ARGV[1]); print "ther prod is $rtn"; } sub add{ my ($sum); $sum=0; foreach $_(@_) { $sum +=$_; } return $sum; } sub multiply { my ($multiply); $product=0; foreach $_(@_) { $product+=$_; } return $multiply: } am i doing it right?
        Your sub add is OK, but multiply seems to be just a copy of add, so it will not multiply.

        You have also continued to ignore previous good advice, starting from mrborisguy's - You need 3 arguments to your program - the Operation to perform and at least 2 operands.

        We will continue to help you, and it is OK to post more code and questions here - in fact that is encouraged - but you need to show some effort, and regard for good advice.

             "Income tax returns are the most imaginative fiction being written today." -- Herman Wouk