【问题标题】:Output to a csv file using Text::CSV_XS使用 Text::CSV_XS 输出到 csv 文件
【发布时间】:2013-10-02 15:54:46
【问题描述】:

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

【问题讨论】:

    标签: perl csv


    【解决方案1】:

    您将值推送到@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]]

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      引用Text::CSV_XSprint函数的CPAN文档

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-10
        • 2019-05-17
        • 1970-01-01
        • 2020-09-21
        • 1970-01-01
        • 2018-10-29
        • 2013-05-17
        • 1970-01-01
        相关资源
        最近更新 更多