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

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.

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

    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.

      I delated "and" and I obtained
      Global symbol "$index" requires explicit package name at /home/../scri +pts/creaNexusMatrix.pl line 12. Execution of /home/../scripts/creaNexusMatrix.pl aborted due to compil +ation errors.
      so I added
      my $index;
      and the code began;
      my @column; my $index; my $curr_column = 0; my $rows = 0; while( @array) { 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"; };
      Now the script works but I didn't obtain that I hope... I obtained:
      -- 0.0175 0.0483 0.0507 -- 0.0471 0.0483 -- 0.0287 --

        Try

        #!perl use strict; my @array = qw( -- 0.0175 0.0483 0.0507 -- 0.0471 0.0483 -- 0.0287 --); my @column; my $curr_column = 0; my $rows = 0; while (@array) { if( $array[0] ne '--' ) { push @{ $column[ $curr_column ] }, shift @array; if( $rows < $#{ $column[ $curr_column ] } ) { $rows = $#{ $column[ $curr_column ] }; }; } else { $curr_column++; $column[ $curr_column] ||= [0]; # a new column shift @array; }; }; for my $row (0..$rows) { my @tmp = (); for (@column){ if ( exists ($_->[ $row - $rows -1 ]) ){ push @tmp,$_->[ $row - $rows - 1 ]; } } print join "\t", @tmp; print "\n"; };
        poj