画面への正常な印刷を行う perl スクリプトがありますが、出力を csv ファイルにリダイレクトしようとすると、次のエラーが発生しますExpected fields to an array ref
。私が使用Text::CSV_XS
していて、エラーを与える行は$csv->print ($fh, $_) for @rows;
#!/user/local/bin/perl
use Text::CSV_XS;
$|=1;
sub main {
print "Enter file to process: ";
my $file = <STDIN>;
chomp $file;
my @rows;
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 });
open(INPUT, $file) or die("Input file $file not found.\n");
while(my $line = <INPUT>) {
if($line =~ /Assay/) {
@words = split(" ",$line);
push @rows, $words[1];
}
if($line =~/Date/) {
@words = split(" ",$line);
push @rows, $words[1];
push @rows, $words[2];
}
if($line =~/Patient/) {
@words = split(" ",$line);
push @rows, $words[0];
push @rows, $words[1];
push @rows, $words[2];
}
if($line =~/channel_index/) {
print $line;
}
if($line =~/Channel/) {
@words = split(" ",$line);
push @rows, $words[1];
push @rows, $words[2];
}
if($line =~/DCMean/) {
@words = split(" ",$line);
push @rows, $words[0];
push @rows, $words[1];
}
}
$csv->eol ("\r\n");
open $fh, ">:encoding(utf8)", "new.csv" or die "new.csv: $!";
$csv->print ($fh, $_) for @rows;
close $fh or die "new.csv: $!";
close(INPUT);
}
main();