Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: storing a file in 2d array

by rnewsham (Curate)
on May 01, 2020 at 08:07 UTC ( #11116299=note: print w/replies, xml ) Need Help??


in reply to storing a file in 2d array

If your data is tab separated you should split on a \t.

If you want your sum loop to count the number of elements in a row a better way would be

for ( @$row ) { $sum++; }

If you want to print ';' separated you can just use a join. Although if you want this as input to some other program it may be safer to look at something like Text::CSV and set semicolon as the sep_char

print join( '; ', @$row );

Couple of best practice notes; use warnings is preferred over -w and use 3 argument open.

Putting it all together, not sure it is exactly what you want but should help get you there.

#!/usr/bin/perl use strict; use warnings; open my $fh, '<', "/tmp/Gene.txt" or die $!; my @content = (<$fh>); close($fh); my @myArray; for my $row (@content) { my @columns = split '\t', $row; push @myArray, \@columns; } my $title_row = shift @myArray; for my $row (@myArray) { my $sum = 0; for ( @$row ) { $sum++; } print "$row->[0] is $sum\n"; print join( '; ', @$row ); }

Replies are listed 'Best First'.
Re^2: storing a file in 2d array
by AnomalousMonk (Bishop) on May 01, 2020 at 10:15 UTC
    ... count the number of elements in a row ...

    To accumulate a sum of the number of elements in a referenced array, a better way IMHO would be
        $sum += @$row;


    Give a man a fish:  <%-{-{-{-<

Re^2: storing a file in 2d array
by shabird (Sexton) on May 01, 2020 at 13:28 UTC

    Works! and it does what i want thank you :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2020-11-24 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?