How can I sum each column of my data in Perl?
I h开发者_JAVA百科ave a file with following data:
1==0==2 5==3==2 7==1==0
how to add the numerical value column wise. I need to summarize and print it like
1==0==2 5==3==2 7==1==0 13==4==4 * summation column wise (This is what I want to calculate using perl)
I guess you have misunderstood my question. I have edited my question again by ... i meant I have many such column in the table 13==4==4 is the summation column wise which I want add to my file.
I was able to do it for first column only but i need to learn how to for all the other columns as well.my code:
#!/usr/bin/perl
use strict;
use warnings;
open (TEMPTABLE,"temp_data") or die "Cannot open file\n";
my @temp_table_data=< TEMPTABLE > ;
chomp @temp_table_data;
my $total_sum;
for(my $i=0;$i<=$#temp_table_data;$i++)
{
print "$temp_table_data[$i]\n";
my @col=split('==',$temp_table_data[$i]);
for(my $m=0;$m<1;$m++)
{
$total_sum+=$col[$m];
}
}
print "$total_sum\n";
OUTPUT:
1==0==2
5==3==2
7==1==0
13
I don't want to sum ROW but COLUMN.
People are trying to be pretty clever in their answers. I think it's much more clear what's going on without the tricks. You certainly don't need pairwise
, and I think in this case it makes the code harder to follow. This is simple with just built-in Perl:
my @sums;
while( <DATA> ) {
my @summands = split /==/;
foreach my $i ( 0 .. $#summands ) {
$sums[$i] += $summands[$i];
}
}
print "Sums are (@sums)\n";
__END__
1==0==2
5==3==2
7==1==0
What you're trying to do does not seem that complex. If '=='
is your column delimiter:
use strict;
use warnings;
use List::MoreUtils qw<pairwise>;
our ( $a, $b );
my @totals;
while ( my $record = <DATA> ) {
chomp $record;
my @data = split /==/, $record;
push @totals, ( 0 ) x ( @data - @totals ) if @data > @totals;
pairwise { $a += $b } @totals, @data;
}
__DATA__
1==0==2
5==3==2
7==1==0
13==4==4
- Create a variable that can hold an array
- Go through each line and sequentially add the values to your array
- Repeat until done.
Here's my entry
use strict;
use warnings;
my @LineTotalsArray;
while (my $line = <DATA>) {
print $line;
chomp $line;
my $index=0;
for my $val ( split /==/, $line ) {
$LineTotalsArray[ $index++ ] += $val;
}
}
print join('==', @LineTotalsArray), "\n";
__DATA__
1==0==2
5==3==2
7==1==0
精彩评论