in reply to Summing numbers

Hi, you need to gather the input into a variable first, and then see if there is more than one number per line entered, something like this (adds a little error checking too):

use strict; use warnings; use feature 'say'; use Scalar::Util 'looks_like_number'; say "Please input numbers to be summed. Crtl-D to stop"; my $user_input; while ( <STDIN> ) { $user_input .= $_; } total( $user_input ); sub total { my $in = shift or die 'No numbers provided!'; my $user_total = 0; for my $line ( split "/n", $in ) { chomp $line; for my $num ( split /\W+/, $line ) { die "$num doesn't look like a number!" if not looks_like_n +umber( $num ); $user_total += $num; } } say "Total: $user_total"; } __END__
Output:
$ perl 1226028.pl Please input numbers to be summed. Crtl-D to stop 12 12 Total: 24 $

Hope this helps!


The way forward always starts with a minimal test.