CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Subroutines

 



Cheral
stranger

Nov 2, 2001, 12:33 PM

Post #1 of 2 (295 views)
Subroutines Can't Post

I'm trying to write a program that will have two subroutines add() and multiply(), add or multiply its arguments and print the results on screen.

The command line invocations should look like:

proj2_4.pl add 1 2

<or>

proj2_4.pl add 23 78 2 99
(In this case the code will add the values together and print the results on screen)



Coderifous
journeyman

Nov 2, 2001, 8:54 PM

Post #2 of 2 (289 views)
Re: Subroutines [In reply to] Can't Post


Code
 

add() if ($a=shift(@ARGV)) =~ /^add$/i;
multiply() if $a =~ /^multiply$/i;

sub add {
for (@ARGV){ $sum += $_ if /^\d+$/; }
print "Sum is $sum\n";
}
sub multiply {
$prod = 1;
for (@ARGV){ $prod *= $_ if /^\d+$/; }
print "Product is $prod\n";
}



Here is the script. Wink
Hope it helps.

--Jim


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives