catfish1116 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to sum a group of numbers from user input and am getting errors
say "Please input numbers to be summed. Crtl-D to stop"; my $user_total = &total(<STDIN>); chomp($user_total); #print "The total of inputted numbers is $user_total \n"; sub total { state $sum = 0; state $number; foreach my $number (@_) { $sum += $number; } return $sum; } ~ Please input numbers to be summed. Crtl-D to stop 12 12 Argument "12 12\n" isn't numeric in addition (+) at ./Chapter_Exer1A l +ine 17, <STDIN> line 1.
I can get this to work if I'm just reading from a hard coded list, but getting user input is not working for me. TIA Catfish
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Summing numbers
by davido (Cardinal) on Nov 19, 2018 at 22:05 UTC | |
Re: Summing numbers
by 1nickt (Canon) on Nov 19, 2018 at 22:03 UTC | |
Re: Summing numbers
by AnomalousMonk (Archbishop) on Nov 20, 2018 at 00:09 UTC | |
Re: Summing numbers -- oneliner
by Discipulus (Canon) on Nov 20, 2018 at 10:39 UTC | |
Re: Summing numbers
by BillKSmith (Monsignor) on Nov 20, 2018 at 15:35 UTC |