Nathan_84 has asked for the wisdom of the Perl Monks concerning the following question:
I have a simple calculator script which I have been playing around with. At the moment it can work out two arguments. I'm trying to edit the script so it can work out three arguments. Please can someone help. Thanks
-------------------------#!/usr/bin/perl $nbArguments = $#ARGV + 1; print "number of arguments: $nbArgumentsn"; exit(1) unless $nbArguments == 3; $a = $ARGV[0]; $b = $ARGV[2]; $operation = $ARGV[1]; if ($operation eq "+") {$result = $a + $b; }elsif ($operation eq "-") {$result = $a - $b; }elsif ($operation eq "/") {$result = $a / $b; }elsif ($operation eq "x") {$result = $a * $b; }print "$a $operation $b = $result \n"; --------------------------------------- This is what I tried and failed! #!/usr/bin/perl $nbArguments = $#ARGV + 1; print "number of arguments: $nbArgumentsn"; exit(1) unless $nbArguments == 5; $a = $ARGV[0]; $operation = $ARGV[1]; $b = $ARGV[2]; $c = $ARGV[3]; if ($operation eq "+") {$result = $a + $b + $c; }elsif ($operation eq "-") {$result = $a - $b; }elsif ($operation eq "/") {$result = $a / $b; }elsif ($operation eq "x") {$result = $a * $b; }print "$a $operation $b $operation $c = $result \n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: calculator script
by toolic (Bishop) on Apr 29, 2010 at 01:17 UTC | |
|
Re: calculator script
by MidLifeXis (Monsignor) on Apr 29, 2010 at 13:26 UTC | |
|
Re: calculator script
by kiruthika.bkite (Scribe) on Apr 29, 2010 at 04:12 UTC | |
|
Re: calculator script
by planetscape (Chancellor) on Apr 29, 2010 at 01:40 UTC | |
|
Re: calculator script
by AR (Friar) on Apr 29, 2010 at 13:33 UTC | |
by Nathan_84 (Acolyte) on May 01, 2010 at 00:28 UTC |