Perl: the Markov chain saw PerlMonks

### sum of integers in a column

by \$new_guy (Acolyte)
 on Oct 12, 2010 at 12:15 UTC Need Help??
\$new_guy has asked for the wisdom of the Perl Monks concerning the following question:

HiMonks,

How do I get the sum of integers in a column

for example:

```2 3
3 4
4 5
5 6
The sum of the integers in the columns is: 14 18

My data is:

```69 44 32 30 32 30 29 29 28
53 61 31 50 31 28 28 28 28
56 46 56 37 29 29 30 29 28
69 45 37 29 30 29 29 29 28
72 65 44 33 29 28 28 29 28
76 47 38 42 30 34 29 29 28
53 46 33 32 29 31 30 28 28
56 38 42 35 29 31 29 29 28
72 65 32 35 29 30 30 28 28
43 44 42 50 30 31 31 29 28
53 35 33 31 34 29 31 29 28
76 45 33 37 36 34 29 28 28
72 55 42 53 31 31 29 28 28
69 40 36 30 30 28 30 28 28

My script so far:

```#!usr/bin/perl -w

use integer;
shift;
while (<>) { \$tot += \$_ }
print "\$tot\n";

Replies are listed 'Best First'.
Re: sum of integers in a column
by muba (Priest) on Oct 12, 2010 at 12:25 UTC

You're on the right track, but you're keeping only one total, whereas there are more than one columns. Suddenly I have this nagging feeling that an array to keep the sums would be useful.

A very small hint:

```while (my \$line = <>) {
my @column = split(' ', \$line);
#    \$sum[\$_] += \$column[\$i] for 0..\$#column;
# line corrected by mjscott7202 into:

\$sum[\$_] += \$column[\$_] for 0..\$#column;

}

That's two hints, actually. Or three, depending on how you look at it. Anyway, on a side note: you'll also want to use strict; and use warnings;. This will help you catch a lot of errors, typoes, and other mistakes.

Shouldn't that be:  \$sum[\$_] += \$column[\$_] for 0..\$#column;

I presume the \$i should actually be the default index variable \$_ generated by the for loop?

You're quite right. Initially I wrote it with a for my \$i (...) {...} loop but then thought I could do it with a postfix for. Forgot to replace that one instance of \$i with \$_. Corrected. Thank you :)

Re: sum of integers in a column
by JavaFan (Canon) on Oct 12, 2010 at 13:49 UTC
perl -anE '\$s[\$_]+=\$F[\$_]for 0..\$#F;END{say"@s"}' data
Re: sum of integers in a column
by snape (Pilgrim) on Oct 12, 2010 at 16:41 UTC

This should work

```#!/usr/bin/perl

use strict;
use warnings;

open IN, 'data.txt' or die\$!;
my @colSum;

while(<IN>){
chomp(\$_);
my @colArray = split(/\s/,\$_);
for(my \$i = 0; \$i <= \$#colArray; \$i++){
\$colSum[\$i] += int(\$colArray[\$i]);
}
}

foreach my \$ j(0 .. \$#colSum){
print "Sum for column",\$j,"is: ",\$colSum[\$j],"\n";
}

What you were trying to do was not correct. You were taking the entire line and was adding to another line. \$_ takes it as a string and if you convert into a int type also then it will not convert according to your logic because there will be spaces between them. Therefore, you need to take it into an array and compute the sum of column by adding to the previous element using loop.

Re: sum of integers in a column
by kcott (Chancellor) on Oct 12, 2010 at 21:40 UTC

Removed!

Sorry, I misread the question. :-(

OK, (after reading the question properly) here's another way to do it. :-)

```#!perl

use 5.12.0;
use warnings;

use List::Util qw{sum};

my @table = ();

while (<DATA>) {
push @table, [ split ];
}

for my \$col (0 .. \$#{\$table[0]}) {
say qq{Column \$col = }, sum map { \$table[\$_][\$col] } (0 .. \$#table
+);
}

__DATA__
1 2 3 4 5 6
1 2 3 4 5 6

Output:

```\$ column_sum.pl
Column 0 = 2
Column 1 = 4
Column 2 = 6
Column 3 = 8
Column 4 = 10
Column 5 = 12

Update: My apologies if anyone noticed this kept changing and wondered what the hell was going on. My main intent was just to highlight the sum function in List::Util.

-- Ken

It has been pointed out to me that I should have used <strike> instead of simply deleting the code I originally posted.

My original solution calculated the sum of the column items in each row rather than the sum of each column. The comment was that even though this didn't answer the OP's question it was nonetheless interesting code that others might like to see.

So, rather than doing any more (possibly confusing) updates to my last post, I've posted my original offering separately here.

```#!perl

use 5.12.0;
use warnings;

use List::Util qw{sum};

my \$total = 0;

while (<DATA>) {
\$total += sum split;
}

say \$total;

__DATA__
1 2 3 4 5 6
1 2 3 4 5 6

Outputs:

```\$ row_sum.pl
42

Enjoy!

-- Ken

Re: sum of integers in a column
by sbmp111 (Novice) on Oct 12, 2010 at 14:27 UTC

#!/usr/bin/perl ##Assume the following array having data in the matrix form. @a=( qw(1 2 3), qw(1 2 3), qw(1 2 3) ); for (my \$j=0;\$j <= (\$#{\$a[0]}) ;\$j++) { my \$sum=0; print "=======\n"; for(my \$i=0;\$i<=\$#a;\$i++) { print "\$i\$j=\$a\$i\$j \n"; \$sum+=\$a\$i\$j; } push @a, \$sum; print "\n\$sum\n"; } print "\naaaa===\n@a\n";

```#!/usr/bin/perl
##Assume the following array having data in the matrix form.
@a=(
[qw(1 2 3)],
[qw(1 2 3)],
[qw(1 2 3)]
);

for (my \$j=0;\$j <= (\$#{\$a[0]}) ;\$j++)
{
my \$sum=0;
print "=======\n";
for(my \$i=0;\$i<=\$#a;\$i++)
{
print "[\$i][\$j]=\$a[\$i][\$j] \n";
\$sum+=\$a[\$i][\$j];
}
push @a, \$sum;
print "\n[\$sum]\n";
}
print "\naaaa===\n@a\n";

Create A New User
Node Status?
node history
Node Type: perlquestion [id://864834]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-01-23 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (240 votes). Check out past polls.

Notices?