#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5

    Beginning problems with multiple arguments


    I'm trying to write a command whose code is determined by the number of arguments the user inputs. Each argument is to be a number. The program is supposed to multiply, add, and subtract all of the arguments whether there are two, three, four, five, etc arguments. How do I use the user inputs? How do I code for an array of indeterminate length?

    Basically, the user inputs a set of numbers of any size. The programs calculates the sum, product, and the difference of the numbers.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Try running this program with command line arguments 1 2 3 4 5 6 7

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    my $sum = 0;
    
    foreach ( @ARGV )
    {
            $sum += $_;
            print $sum, "\n";
    }
    
    __END__
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Arguments will automatically be stored in the @ARGV array.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Everything has been said, just one last point: in Perl, an array will automatically grow to the size required to store all its elements. This of course applies to the @ARGV array, whose size will automatically be the number of arguments passed to the script.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5

    thanks


    Thanks, now what if I wanted to take the difference of all of the numbers in a set.

    I keep getting an error message when I try it.



    How do I post screen shots to the message board?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    The difference of all numbers does not make any sense in mathematics. You've got to explain more precisely what your substraction operands will be.

    Otherwise, if you have an error in your script, post the script as well as the error message you received when you tried to run it, we will probably be able to help you sorting it out.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5
    Originally Posted by Laurent_R
    The difference of all numbers does not make any sense in mathematics. You've got to explain more precisely what your substraction operands will be.

    Otherwise, if you have an error in your script, post the script as well as the error message you received when you tried to run it, we will probably be able to help you sorting it out.
    By difference, I mean if the user inputs the numbers 6,7,8,7, the program will calculate 6-7-8-7=-16.

    The code I have is as follows:


    #!/usr/bin/perl
    use strict;
    use warnings;

    my $C=scalar (@ARGV);
    my @inp=@ARGV;
    if ($C>2) {
    print "There are more than two arguments! \n";
    }

    my $Sum=0;
    print "The values entered are @ARGV \n";
    foreach( @ARGV )
    {
    $Sum += $_;
    }
    print "The sum of the values is $Sum,";
    print "\n";
    #I had assistance for this part
    my $Difference=$ARGV[1];
    my $i=2;
    for ($i=2; $i=$C; $i++) {
    $Difference=$Difference-$ARGV[$i];
    }
    if ($Difference<0) {
    print "The difference of all of the arguemnts is a negative number.";
    }else{
    print "$Difference";
    }
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Hi,

    the first element of an array has index 0, not 1. So you should initially assign $difference to $ARGV[0], not $ARGV[1].

    I think your for loop is probably broken, but have no time now to test it. I would rewrite this part of your code like this:

    Perl Code:
    my $difference = shift @ARGV;
    # now $difference holds the first item of the @ARGV array and 
    # this value has been removed from the array, so that the array
    # now contains only the operands to be subtracted.
     
    foreach my $value (@ARGV) {
         $difference -= $value;
    }
    # do the printing job


    A side note:

    Code:
    my @inp=@ARGV;
    is useless, since you don't use the new array.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    See if your instructor likes this one.

    Code:
    #!/usr/bin/perl
    
    use 5.10.0;
    use strict;
    use warnings;
    
    for my $operator ( qw/ + * - / ){ say eval join $operator, @ARGV; }
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    5

    Smile


    Originally Posted by FishMonger
    See if your instructor likes this one.

    Code:
    #!/usr/bin/perl
    
    use 5.10.0;
    use strict;
    use warnings;
    
    for my $operator ( qw/ + * - / ){ say eval join $operator, @ARGV; }
    It doesn't compile in my version of Perl, by nice trick anyways.


    Originally Posted by Laurent_R
    Hi,

    the first element of an array has index 0, not 1. So you should initially assign $difference to $ARGV[0], not $ARGV[1].

    I think your for loop is probably broken, but have no time now to test it. I would rewrite this part of your code like this:

    Perl Code:
    my $difference = shift @ARGV;
    # now $difference holds the first item of the @ARGV array and 
    # this value has been removed from the array, so that the array
    # now contains only the operands to be subtracted.
     
    foreach my $value (@ARGV) {
         $difference -= $value;
    }
    # do the printing job

    Thank you very much for your help. I'm still figuring out the proper syntax here though. For instance, in the code here:
    #!/usr/bin/perl
    use strict;
    use warnings;

    my $C=scalar (@ARGV);
    if ($C>2) {
    print "There are more than two arguments! \n";
    }

    my $Sum=0;
    print "The values entered are @ARGV \n";
    foreach( @ARGV )
    {
    $Sum += $_;
    }
    print "The sum of the values is $Sum,";
    print "\n";
    #I had assistance for this part
    my $Difference=shift @ARGV;
    foreach my $i ( @ARGV ){
    $Difference-=$i;
    }
    if ($Difference<0) {
    print "The difference of all of the arguemnts is a negative number.";
    }
    if($Difference>0){
    print "The difference of the values is $Difference \n";
    }

    my $Product=1;
    foreach ( @ARGV )
    {
    $Product *= $_;
    }
    print "The product of the values is $Product \n";


    The product output does not include the first argument for some reason. Why is this and why won't it output the product of all of the arguments?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    Originally Posted by Astrodude
    It doesn't compile in my version of Perl, by nice trick anyways.
    That's because you're using an old version of perl.

    This version will compile with your old perl, but you'll need to explain to your instructor, without any additional help, how it works.
    Code:
    D:\test>perl -e "for ( qw/ + * - / ){ print eval join $_, @ARGV; print $/}" 3 6 9
    Last edited by FishMonger; October 20th, 2012 at 12:14 AM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    Originally Posted by Astrodude
    (...)
    Code:
    my $Difference=shift @ARGV;
    foreach my $i ( @ARGV ){
        $Difference-=$i;
    }
    if ($Difference<0) {
        print "The difference of all of the arguemnts is a negative number.";
    }
        if($Difference>0){
          print "The difference of the values is $Difference \n";
        }
    
    my $Product=1;
    foreach ( @ARGV )
    {
        $Product *= $_;
    }
    print "The product of the values is $Product \n";

    The product output does not include the first argument for some reason. Why is this and why won't it output the product of all of the arguments?
    Because the shift instruction in the calculation of the difference has removed the first argument from the @ARGV array.

    If you need to use again the @ARGV array, the difference calculation, as currently coded, should work on a copy of @ARGV, in order not to alter @ARGV.

    Example of product calculation under the Perl debugger:

    Perl Code:
      DB<12> $product = 1;
     
      DB<13> $product *= $_ foreach (5, 4, 7);
     
      DB<14> print $product
    140


    As you can see, the product is correct and uses all three arguments passed to the calculation.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    @ARGV > 1 or die "insuficient number of args\n";
    
    if (grep /\D/, @ARGV) {
        die "only postive integers are allowed\n";
    }
    
    add(@ARGV);
    subtract(@ARGV);
    multiply(@ARGV);
    
    
    sub add {
        my @numbers = @_;
        # code that adds the numbers and ouputs the total
    }
    
    sub subtract {
        my @numbers = @_;
        # code that subtracts the numbers and ouputs the total
    }
    
    sub multiply {
        my @numbers = @_;
        # code that mulitplies the numbers and ouputs the total
    }

IMN logo majestic logo threadwatch logo seochat tools logo