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

ok i have to do this. 2 subroutines that each take a list of numbers (six numbers in my case) as a parameter. 1st: adds up all the numbers in list, and returns total, and 2nd is returns the highest number in the list. use warnings; use strict; sub AddNumbers { numberList($first_number, $second_number, $third_number, $fourth_number, $fifth_number, $sixth_number) = @_; $add $first_number + $second_number + $third_number + $fourth_number + $fifth_number + $sixth_number; } print "Equals to: &add /n" this is what im doing so far, it gives me errors, what am i doing wrong?

Replies are listed 'Best First'.
Re: Subroutine help
by jwkrahn (Abbot) on Aug 23, 2009 at 04:25 UTC
    1st: adds up all the numbers in list, and returns total

    Use the  sum function from the List::Util module.

    2nd is returns the highest number in the list

    Use the  max function from the List::Util module.

Re: Subroutine help
by tmharish (Friar) on Aug 23, 2009 at 07:21 UTC
    Like everyone has pointed out "code" tags will make your code clearer.
    Here is what I get when I clean out your code
    use warnings; use strict; sub AddNumbers { numberList($first_number, $second_number, $third_number, $fourth_n +umber, $fifth_number, $sixth_number) = @_; $add $first_number + $second_number + $third_number + $fourth_numb +er + $fifth_number + $sixth_number; } print "Equals to: &add /n"
    And here are the errors that your code generates:
    Scalar found where operator expected at temp.pl line 7, near "$add $fi +rst_number" (Missing operator before $first_number?) Global symbol "$first_number" requires explicit package name at temp.p +l line 6. Global symbol "$second_number" requires explicit package name at temp. +pl line 6. Global symbol "$third_number" requires explicit package name at temp.p +l line 6. Global symbol "$fourth_number" requires explicit package name at temp. +pl line 6. Global symbol "$fifth_number" requires explicit package name at temp.p +l line 6. Global symbol "$sixth_number" requires explicit package name at temp.p +l line 6. Global symbol "$add" requires explicit package name at temp.pl line 7. syntax error at temp.pl line 7, near "$add $first_number " Global symbol "$first_number" requires explicit package name at temp.p +l line 7. Global symbol "$second_number" requires explicit package name at temp. +pl line 7. Global symbol "$third_number" requires explicit package name at temp.p +l line 7. Global symbol "$fourth_number" requires explicit package name at temp. +pl line 7. Global symbol "$fifth_number" requires explicit package name at temp.p +l line 7. Global symbol "$sixth_number" requires explicit package name at temp.p +l line 7. Execution of temp.pl aborted due to compilation errors.

    Let me try explaining what these errors mean:
    Scalar found where operator expected at temp.pl line 7, near "$add $fi +rst_number"

    You will see that you have missed out the '='. Perl is telling you that it was looking for some operator but found another variable.
    Global symbol "something" requires explicit package name at

    Here you need to use 'my' because you have used "strict". If not Perl is going to look for places where this variable has been defined and not finding any will croak.
    print "Equals to: &add /n"
    First off new line is '\n' and not'/n'. I am unclear on what you expect printed here considering you have not called 'AddNumbers'

    Here is your code with the above mentioned errors corrected:
    use strict; use warnings; sub AddNumbers { my ($first_number, $second_number, $third_number, $fourth_number, +$fifth_number, $sixth_number) = @_; my $add = $first_number + $second_number + $third_number + $fourth +_number + $fifth_number + $sixth_number; return $add; } my $sum = AddNumbers( 1, 2, 3, 4, 5, 6 ); print "The sum of input numbers equals to: $sum \n";

    You must note that it is not a good idea to use a static number of params like you have. This will limit your method to a particular number of input parameters (in this case 6). The fact that Perl always passes parameters as a list can be exploited and allows for an easy way to implement polymorphism.

    This is where the solution given by Anonymous Monk comes in. You will notice that that solution will work for any number of parameters.
      Hi, Thank you very much for your help. It works now, but it doesnt let me input the numbers into the parameter, it always equals to 21. I need to be able to input numbers. Thanks to everyone for all your help. :D
        Where's your code to prompt for and read the six numbers? We can't help you if you don't show us your code.
Re: Subroutine help
by ganeshk (Monk) on Aug 23, 2009 at 03:56 UTC
    Please enclose your code in code tags.(<code>..</code>) Also the code for numberList function is missing and $add is not declared. Regarding the print statement you need to give the function call outside of the quotes like this..
    # I believe that you are passing the parameters of current # function to AddNumbers print "Equals to: ", &AddNumbers;
    That's all I can see for now.
Re: Subroutine help
by Anonymous Monk on Aug 23, 2009 at 04:08 UTC
    1. put code in <code> code here </code> tags
    2. copy/paste error message
    3. function calls do not work inside strings, they don't interpolate like variables, so
      printf "Equals to: %s\n", add();
    4. @_ is an array, so just iterate over it
      my $sum = 0; foreach(@_){ $sum += $_; }
Re: Subroutine help
by Marshall (Canon) on Aug 25, 2009 at 03:04 UTC
    I am surprised that your teacher didn't give you enough hints to do this. I am also surprised that your teacher threw in this "red herring" of 6. The subs handle any number of numbers.

    View this as a "homework gift". You will need to work harder for the next one.

    #!/usr/bin/perl -w use strict; my @test = (1,2,3,4,5,6,7,8,9); print sum(@test),"\n"; print max(@test),"\n"; sub sum { my @nums = @_; my $sum =0; foreach my $num (@nums) { $sum+= $num; } return ($sum); } sub max { my @nums = @_; my $max = -9999999999; foreach my $num (@nums) { if ($num>$max) { $max = $num; } } return ($max); } __END__ PRINTS: 45 9
    Ok, a very simple command ui.
      Thanks alot guys!! Thank you Marshal, your code was just what i needed, thanks alot. is there like a thanks button or rep addder in this forum? Anyway, thank you all for your help.