in reply to How can you make this script general?
No real need for Perl references in my view, nor for any complex data structure. A simple array should do the work (if I understood the requirement well).
My only assumption is that the number of columns is equal to or less than 21. This is the resuling outputt;use strict; use warnings; my @sums; $sums[$_] = 0 for 0..20; while (<DATA>) { my @fields = split /\s+/, $_; for (0..20) { $sums[$_] += $fields[$_] if defined $fields[$_]; } } print "@sums", "\n"; __DATA__ 1 2 3 4 6 3 4 5 6 6 7 8 9
$ perl column_sum.pl 10 13 16 19 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
|
|---|