Create CSV file from 2d array perl -
i trying load csv file, transpose , write new one. have working correctly except writing new file. have looked around online without success.
use strict; use warnings; use text::csv; use data::dump qw(dump); use array::transpose; @data; # 2d array csv data $file = 'sample_array.csv'; $csv = text::csv->new; open $fh, '<', $file or die "could not open $file: $!"; while( $row = $csv->getline( $fh ) ) { shift @$row; # throw away first value push @data, $row; } @data=transpose(\@data); dump(@data); the output here transposed array @data (["blah", 23, 22, 43], ["tk1", 1, 11, 15],["huh", 5, 55, 55]). need output written new csv file.
csv file:
text,blah,tkl,huh 14,23,1,5 12,22,11,55 23,42,15,55
refer code after dump. derived text::csv synopsis:
use strict; use warnings; use text::csv; use data::dump qw(dump); use array::transpose; @data; # 2d array csv data $file = 'sample_array.csv'; $csv = text::csv->new; open $fh, '<', $file or die "could not open $file: $!"; while( $row = $csv->getline( $fh ) ) { shift @$row; # throw away first value push @data, $row; } @data=transpose(\@data); dump(@data); open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!"; (@data) { $csv->print($fh, $_); print $fh "\n"; } close $fh or die "new.csv: $!";
Comments
Post a Comment