shabird has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, hope you all are fine
I am struggling with multi dimensional array this time so basically the question goes like this, I have a file which has a data like this
GeneID Tp1 Tp2 Tp3 ALA1 10 12 11 THR8 57 99 12 HUA4 100 177 199 ABA5 2 5 10
So i have read the file and assigned it to multi dimensional array with the help of this code.
my @content = (<FH>); close(FH); my $no_of_seq = scalar(@content); my @myArray; foreach my $row (@content) { my @columns = split(/\s+/,$row); push(@myArray,\@columns); # print "@columns"; } for($row = 0; $row < $no_of_seq; $row++){ for($col = 0; $col < $no_of_seq; $col++){ print($myArray[$row][$col], "\t"); } print("\n"); }
so this code prints every element in the same order but now i want to calculate the sum of numbers in each row and print that sum next to gene name i.e sum of values for ALA1 is 33 and sum of values for THR8 is 168 and so on. Now i have tried something like this.
for($row = 0; $row < $no_of_seq; $row++){ for($col = 0; $col < $no_of_seq; $col++){ $sum = $myArray[$row][$col] + $myArray[$row]; print($sum, "\t"); }
But this is printing something like this
140526414289424 140526414289424 140526414289424 1405264142894 +24 140526414289424 140526414212840 140526414212850 140526414212852 1405264142128 +51 140526414212840 140526414213296 140526414213353 140526414213395 1405264142133 +08 140526414213296 140526414288368 140526414288468 140526414288545 1405264142885 +67 140526414288368 140526414313944 140526414313946 140526414313949 1405264143139 +54 140526414313944
How can i achieve my desired result? thank you in advance
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Query of multi dimentional array
by choroba (Cardinal) on Mar 26, 2020 at 10:09 UTC | |
by bliako (Abbot) on Mar 26, 2020 at 10:43 UTC | |
|
Re: Query of multi dimentional array
by marto (Cardinal) on Mar 26, 2020 at 09:58 UTC | |
by shabird (Sexton) on Mar 26, 2020 at 10:01 UTC | |
|
Re: Query of multi dimensional array
by hippo (Archbishop) on Mar 26, 2020 at 10:24 UTC | |
by shabird (Sexton) on Mar 26, 2020 at 12:41 UTC | |
by hippo (Archbishop) on Mar 26, 2020 at 13:37 UTC | |
|
Re: Query of multi dimentional array
by kcott (Archbishop) on Mar 27, 2020 at 02:01 UTC |