Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
$myArray[$row] is a reference to an array, you seem to be adding it to the actual numbers in the columns. That's why the values are so high. You can simplify the algorithm by iterating over the values rather than indices:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @content = (<DATA>); my $no_of_seq = scalar(@content); my @myArray; for my $row (@content) { my @columns = split ' ', $row; push @myArray, \@columns; } say join "\t", $myArray[0][0], 'sum'; my $width = $#{ $myArray[0] }; for (my $row = 1; $row < $no_of_seq; $row++){ print $myArray[$row][0], "\t"; my $sum = 0; for my $col (@{ $myArray[$row] }[1 .. $width]) { $sum += $col; } say $sum; } __DATA__ GeneID Tp1 Tp2 Tp3 ALA1 10 12 11 THR8 57 99 12 HUA4 100 177 199 ABA5 2 5 10

Update: But, to compute a sum of a row, you don't need the other rows, so you can process the file line by line, no need to store it into an array. List::Util already exports the sum sub, no need to compute it yourself.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ sum }; <DATA>; # ignore the first line; say "GeneID\tSum"; while (<DATA>) { my @cols = split ' '; my $sum = sum(@cols[1 .. $#cols]); say join "\t", $cols[0], $sum; }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Query of multi dimentional array by choroba
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-23 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found