You will need to keep a count of the total per row, but you are not doing so yet. Further your code is trying to add a single value to an array which logically makes no sense. Also, each row contains the gene name as well as the data so you need to avoid trying to add a number to a string.
Using your loop structures then we might try this instead:
for($row = 0; $row < $no_of_seq; $row++){ my $sum = 0; for($col = 1; $col < $no_of_seq; $col++){ $sum += $myArray[$row][$col]; } print "$myArray[$row][0] $sum\n"; }
But that's not very perlish. Let's improve the loops:
for my $row (@myArray) { my $sum = 0; for ($col = 1 .. $no_of_seq - 1) { $sum += $row[$col]; } print "$row[0] $sum\n"; }
This could be further cleaned if you work on a copy of @myArray which you could disrupt as you go. Putting this all together we arrive at the SSCCE:
#!/usr/bin/env perl use strict; use warnings; my @myArray; while (<DATA>) { my @columns = split /\s+/, $_; push @myArray, \@columns; } my $title_row = shift @myArray; for my $row (@myArray) { my $sum = 0; for my $col (1 .. $#$row) { $sum += $row->[$col]; } print "$row->[0] $sum\n"; } __DATA__ GeneID Tp1 Tp2 Tp3 ALA1 10 12 11 THR8 57 99 12 HUA4 100 177 199 ABA5 2 5 10
In reply to Re: Query of multi dimensional array
by hippo
in thread Query of multi dimentional array
by shabird
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |