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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.