http://qs321.pair.com?node_id=11116299


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 (Archbishop) 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 :)