目次 †
内容 †
CSVのパース †
そのまま読み込む †
use Text::CSV_XS;
use IO::File;
my $csv = Text::CSV_XS->new( {binary => 1} );
my $fhIn = IO::File->new( $oneSrcFile ) || die ( "cannot open file: $!" );
while ( not $fhIn->eof ) {
my $columns = $csv->getline( $fhIn ); # 配列のリファレンス
}
CSVの1行目をキーに連想配列を生成 †
use Text::CSV_XS;
use IO::File;
use Data::Dumper;
my $csv = Text::CSV_XS->new( {binary => 1} );
my $fhIn = IO::File->new( $oneSrcFile ) || die ( "cannot open file: $!" );
my @csv_header;
while ( not $fhIn->eof ) {
my $columns = $csv->getline( $fhIn ); # 配列のリファレンス
if( ! @csv_header ){
@csv_header = @$columns;
next;
}
next if ( scalar( @$columns ) != scalar( @csv_header ) );
my %rec = map{ $_ => shift @$columns; } @csv_header ;
print Dumper( \%rec );
}
CSVの出力 †
use Text::CSV_XS;
use strict;
use warnings;
my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/ });
open my $fh, ">", "csv_test.csv";
my $data = [ "ab", "cd", 'aa"bb' ];
$csv->print($fh, $data );
close $fh;