2

我有一个 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();
4

2 回答 2

4

您将值推入 的方式@rows,您将获得一个巨大的、扁平的标量数组。这可能不是你想要的。

考虑以下:

my @rows;
push @rows, 'a';
push @rows, 'b';
push @rows, 'c';
push @rows, 'd';
push @rows, 'e';
push @rows, 'f';

给了我们一个平面数组:[a,b,c,d,e,f].

这在哪里:

my @rows;
push @rows, ['a', 'b', 'c'];
push @rows, ['d', 'e', 'f'];

给我们一个嵌套数组:[[a,b,c], [d,e,f]].

数组和数组引用也很相似,但又不同。请参阅perlreftut。这是一个微妙的概念,但对于高级 Perl 开发至关重要。请阅读并理解!

您的推送代码可能如下所示:

push @rows, [$words[1], $words[2]];

这些[]标量周围创建了一个匿名数组引用。由于@rows现在将填充数组引用,因此您不需要更改任何其他内容。

于 2013-10-02T00:41:14.410 回答
1

尝试将错误报告行更改为以下内容:

$csv->print ($fh, \@rows);

来自Text::CSV_XSCPANprint函数文档的引用

它需要一个数组 ref 作为输入(不是数组!)

于 2013-10-02T00:09:07.563 回答