in reply to Re: array in different columns
in thread array in different columns

No, this isn't a homework but the final part of a script.

The result of my script is an array like this:

@array=(--, 0.0175, 0.0483, 0.0507, --, 0.0471, 0.0483, --, 0.0287, --)

the length of the array can be differ; depend by the input file

so if I print this array I obtain:
print @array; -- 0.0175 0.0483 0.0507 -- 0.0471 0.0483 -- 0.0287 --
Instead I hope to have:
0 0.0175"\t"0 0.0483"\t"0.0471"\t"0 0.0507"\t"0.0483"\t"0.0287"\t"0
So I tried to insert the array inside a variable and then split it into array
my $fileAverage = join "", @array; $fileAverage =~ s/--\n/--0\n/g; my (@matrix) = split('--', $fileAverage); foreach my $lane (@matrix) {print "\t$lane"; }
but I obtain:
"\t"0 0.0175 0.0483 0.0507 "\t"0 0.0471 0.0483 "\t"0 0.0287 "\t"0

Replies are listed 'Best First'.
Re^3: array in different columns
by Corion (Patriarch) on Nov 23, 2016 at 10:33 UTC

    Maybe you want to think of it in a different way, by looking at how to divide the array across columns. If you build your columns and then print every row of your column, you should get your result. I'm going to first show you how to do it for a fixed number of columns and then how to expand that to an arbitrary number of columns:

    my @column1; my @column2; my @column3; my @column4; while( @array and $array[0] ne '--') { push @column1, shift @array; }; shift @array; while( @array and $array[0] ne '--') { push @column2, shift @array; }; shift @array; while( @array and $array[0] ne '--') { push @column3, shift @array; }; shift @array; while( @array and $array[0] ne '--') { push @column4, shift @array; }; shift @array; for my $row (1..3) { print join "\t", $column1[$row], $column2[$row],$column3[$row],$co +lumn4[$row]; print "\n"; };

    Now, that only works for four columns. To make it work with any number of columns, we need to move away from the named column arrays and store all column arrays in another array ("AoA"):

    my @column; my $curr_column = 0; my $rows = 0; while( @array and ) { if( $array[0] ne '--' ) { $column[ $curr_column ] ||= []; # a new column push @{ $column[ $curr_column ] }, shift @array; if( $rows < $#{ $column[ $curr_column ] } ) { $rows = $#{ $column[ $curr_column ] }; }; } else { $index++; shift @array; }; }; for my $row (0..$rows) { print join "\t", map { $_->[ $row ] } @column; print "\n"; };
      Thank you but the script doesn't work...I obtaine:
      syntax error at /home/.../scripts/creaNexusMatrix.pl line 87, near "an +d ) " syntax error at /home/.../scripts/creaNexusMatrix.pl line 94, near "}" Global symbol "$index" requires explicit package name at /home/.../scr +ipts/creaNexusMatrix.pl line 95. syntax error at /home/.../scripts/creaNexusMatrix.pl line 97, near "}" Execution of /home/.../scripts/creaNexusMatrix.pl aborted due to compi +lation errors.

        The code I posted doesn't even have 87 lines, so I'm not really sure what you expect me to do here.

        Looking over the code I actually posted, there is a trivial syntax error on line 5. There is a stray and there. Removing this makes my code compile without error.

        Maybe you can apply a similar technique to your code.

Re^3: array in different columns
by Muflone82 (Novice) on Nov 23, 2016 at 10:38 UTC

    I also tried with this code

    my $count3="-1"; foreach my $lane (@array) { if ($lane eq "--\n"){$count3++;print "\t" x $count3 . "0\n";} else {print "\t" x $count3 . "$lane"} }
    but I obtained:
    0 0.0175 0.0483 0.0507 "\t" 0 "\t" 0.0471 "\t" 0.0483 "\t\t" 0 "\t\t" 0.0287 "\t\t\t" 0