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

Hi all,

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";
-------------------------
I've got a feeling its to do with the top part of the code and the print command at the bottom but I'm having no joy. Any help would be great. Thanks

Replies are listed 'Best First'.
Re: calculator script
by toolic (Bishop) on Apr 29, 2010 at 01:17 UTC
    I'm trying to edit the script so it can work out three arguments. Please can someone help.
    You neglected to mention how it does not work for you. So, here is my guess. If you change:
    exit(1) unless $nbArguments == 5;

    to:

    exit(1) unless $nbArguments == 4;

    Then execute your code like this:

    C:\Perl> 837421.pl 1 + 3 4 number of arguments: 1 + 3 + 4 = 8

    It seems to add the 3 numbers. The key is to count the number of arguments properly.

    Here are a few more tips:

    • Use perltidy to neaten up the code formatting so that it is easier for you (and especially others) to understand.
    • use strict and warnings so that you can catch typos like $nbArgumentsn.
    • Don't name your variables $a and $b because they have special meaning (see sort).
    • Add some usage documentation (preferably using POD) so that you remember how to run your code 6 months from now.
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
    The problem is ,in the following .
    $nbArguments = $#ARGV + 1; print "number of arguments: $nbArgumentsn"; exit(1) unless $nbArguments == 5;

    Here you are checking the number of arguments is five.
    So it will fail if you are giving input in the following format.
    1 + 2 3
    So that time you need to check the number of arguments is equal to four.
    If you want to give input like,
    1 + 2 + 3
    then that time you need to check the number of arguments is equal to five.And you need to change the following part of code also.
    your code,
    $a = $ARGV[0]; $operation = $ARGV[1]; $b = $ARGV[2]; $c = $ARGV[3]
    Change into,
    $val1 = $ARGV[0]; $operation1 = $ARGV[1]; $val2 = $ARGV[2]; $operation2 = $ARGV[3] $val3=$ARGV[4]

    These changes are needed only in the case of if you are giving input in the format 1 + 2 + 3 .
Re: calculator script
by planetscape (Chancellor) on Apr 29, 2010 at 01:40 UTC

    Instead of:

    print "number of arguments: $nbArgumentsn";

    don't you want:

    print "number of arguments: $nbArguments\n";

    ?

    HTH,

    planetscape
Re: calculator script
by AR (Friar) on Apr 29, 2010 at 13:33 UTC

    Hi Nathan_84,

    What language are you coming from? It feels like you're trying to shoehorn Perl into how you're already comfortable coding. While "There's More Than One Way To Do It", you may benefit greatly by coming to Perl with a blank slate and browsing PM's Tutorials.

    I could also be way off in my assumptions. This might be how someone is teaching you or how you are teaching yourself. If that's the case, I still recommend the Tutorials to see how people very familiar with Perl write and teach in Perl.

    Good luck!

      Hi AR and everybody else. I just want to say thanks for all your help. It's greatly appreciated. And yes AR you partly right. My programming skills are lacking. At the time of writing this post I'm going through various tutorials to teach myself. I think I was trying to run before I could walk! Thank you all again for your help! Nathan