in reply to Summing numbers in a file
Hello pvfki,
When I run your code as written, it works as expected with no errors or warnings. I note that the uninitialized message refers to “line 17” which doesn’t correspond to the code you’ve shown. So I don’t know what the actual problem may be.
A few observations:
Extending the script to process multiple lines just requires a loop:
use warnings 'all'; use strict; use autodie; my $filename = 'numbers.txt'; my $outfile = 'sum.txt'; open(my $in, '<', $filename); open(my $out, '>', $outfile); while (my $numbers = <$in>) { chomp $numbers; my $sum = eval $numbers; print $out $sum, "\n"; } close $out; close $in;
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Summing numbers in a file
by jcb (Parson) on May 31, 2020 at 01:01 UTC | |
by Athanasius (Archbishop) on May 31, 2020 at 07:07 UTC | |
by jcb (Parson) on Jun 01, 2020 at 02:39 UTC | |
by Athanasius (Archbishop) on Jun 01, 2020 at 07:37 UTC | |
by eyepopslikeamosquito (Archbishop) on Jun 03, 2020 at 10:24 UTC | |
by haukex (Archbishop) on May 31, 2020 at 09:18 UTC | |
by jcb (Parson) on Jun 01, 2020 at 02:59 UTC | |
by haukex (Archbishop) on Jun 01, 2020 at 06:17 UTC | |
by jcb (Parson) on Jun 02, 2020 at 03:53 UTC | |
|