in reply to Transpose of an array

$fields[$y] is value of one of the fields of the current rec/line, but you're trying to use it as an array (@{$fields[$y]}).

Something is very wrong with your approach since you're printing before reading the entire file, but you don't have the information you need for the first row before you read the entire file.

Solution:

my @transposed; while (my $row = $csv->getline($FILE1)) { for (0..$#$row) { push @{ $transposed[$_] }, $row->[$_]; } } $csv->print($FILE2, $_) for @transposed;

Replies are listed 'Best First'.
Re^2: Transpose of an array
by bluray (Sexton) on Oct 13, 2011 at 20:07 UTC
    Hi,

    Thanks for the code. It worked. In fact, yesterday, I wrote a similar type of code (which worked fine-pasted below). But, I was trying to get it done through two dimensional arrays.

    my $tfields; while (my $row=$csv->getline($FILE1)) { for (my $i = 0; $i < @$row; $i++){ push (@{$tfields -> [$i]}, $row ->[$i]); } } foreach my $row (@$tfields) { $csv->print ($FILE2, $row); }
      huh? It does build a 2d array.