Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Query of multi dimensional array

by hippo (Bishop)
on Mar 26, 2020 at 10:24 UTC ( [id://11114664]=note: print w/replies, xml ) Need Help??


in reply to Query of multi dimentional array

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

Replies are listed 'Best First'.
Re^2: Query of multi dimensional array
by shabird (Sexton) on Mar 26, 2020 at 12:41 UTC
    Unfortunately your solution isn't printing anything to the terminal :(

      Sure it does:

      $ cat bio.pl #!/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 $ ./bio.pl ALA1 33 THR8 168 HUA4 476 ABA5 17 $ perl -v This is perl 5, version 20, subversion 3 (v5.20.3) built for x86_64-li +nux-thread-multi (with 16 registered patches, see perl -V for more detail) Copyright 1987-2015, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. $

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found