Perl 使用Text::CSV根据内容变量在CSV文件上打印一行

Perl 使用Text::CSV根据内容变量在CSV文件上打印一行,perl,Perl,我的脚本相当大,但我将在这里简化代码。 假设我创建了一个CSV,我写的标题如下: my $csv = Text::CSV->new ({binary => 1}, eol => "\n"); open(my $out, ">", "$dir/out.csv") or die $!; #create $out->print("CodeA,CodeB,Name,Count,Pos,Orientation\n"); #I write the header 假设我得到了存

我的脚本相当大,但我将在这里简化代码。 假设我创建了一个CSV,我写的标题如下:

my $csv = Text::CSV->new ({binary => 1}, eol => "\n");
open(my $out, ">", "$dir/out.csv") or die $!; #create
$out->print("CodeA,CodeB,Name,Count,Pos,Orientation\n"); #I write the header
假设我得到了存储在不同变量中的一些值,我想将这些变量作为一行写入CSV。
我不知道怎么做,因为在Text::CSV文档中,打印没有明确解释,没有直接的示例,我也不知道数组引用是什么。

对于可以使用的标题

$status = $csv->print ($out,[qw(CodeA CodeB Name Count Pos Orientation)]);
对于一行值,使用

$status = $csv->print ($out,[$valueA,$valueB,$valueName,$valueCount,$valuePos,$valueOrientation]);

下面是一个用于编写CSV文件的简单示例。它根据固定数据生成标题行和数据行

#!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV;

my $csv = Text::CSV->new({binary => 1, eol => $/ })
    or die "Failed to create a CSV handle: $!";

my $filename = "output.csv";

open my $fh, ">:encoding(utf8)", $filename or die "failed to create $filename: $!";

my(@heading) = ("CodeA", "CodeB", "Name", "Count", "Pos", "Orientation");
$csv->print($fh, \@heading);    # Array ref!

my(@datarow) = ("A", "B", "Abelone", 3, "(6,9)", "NW");
$csv->print($fh, \@datarow);    # Array ref!

close $fh or die "failed to close $filename: $!";
数据行收集在一个数组中-我使用了
@heading
@datarow
。如果我要输出几行,那么可以在
@datarow
中收集或创建每一行,然后输出。
$csv->print
的第一个参数应该是I/O句柄,这里是输出文件的文件句柄。第二个应该是数组引用。使用
\@arrayname
是创建数组引用的一种方法;Text::CSV模块的输入例程还创建和返回数组引用

请注意
Text::CSV->new
调用中使用的符号与示例中使用的符号之间的差异。还要注意,
$out->print(“…”)调用使用基本文件I/O,与Text::CSV无关。与
$csv->print($fh,…)
形成对比

代码的其余部分或多或少都是样板

output.csv
请注意,带有嵌入逗号的值被Text::CSV模块用引号括起来。其他值不需要引号,因此无法获取它们。您可以使用
Text::CSV->new

#选项调整CSV输出格式的详细信息/usr/bin/perl
#!/usr/bin/perl
use strict;
use warnings;

use Text::CSV;

my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";

my $csv = Text::CSV->new ({
  binary    => 1,
  auto_diag => 1,
  sep_char  => ','    # not really needed as this is the default
});

my $sum = 0;
open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n";
while (my $fields = $csv->getline( $data )) {
  $sum += $fields->[2];
}
if (not $csv->eof) {
  $csv->error_diag();
}
close $data;
print "$sum\n";
严格使用; 使用警告; 使用Text::CSV; my$file=$ARGV[0]或die“需要在命令行上获取CSV文件\n”; my$csv=文本::csv->新建({ 二进制=>1, 自动诊断=>1, sep_char=>','#实际上不需要,因为这是默认值 }); 我的$sum=0;
打开(我的$data),你读过手册了吗?你不了解手册的哪些部分?你知道数组引用是什么吗?这就是重点。我无法理解打印内容,也不知道数组引用是什么。那么你可能应该先阅读。如果你在理解文档时遇到困难,最好(a)链接到它,以及(b)解释你不明白它说的是什么。例如,在
print
的开头附近,它说它需要一个数组ref作为输入(而不是数组!)如果你不明白,就说出来。这表明你对如何查找信息有一点了解,你也做了一些研究,但你有一个问题,因为你不理解所使用的术语。(这也不是世界上最清晰的手册页;
print
is的开头句需要大量的模块知识。)非常感谢我要编辑这个问题。提到
eol=>$/
节省了我的时间,否则它会写在第一行。简单的代码并不是完整的答案。请看一下。问题是如何编写CSV文件。您将演示如何阅读它。
#!/usr/bin/perl
use strict;
use warnings;

use Text::CSV;

my $file = $ARGV[0] or die "Need to get CSV file on the command line\n";

my $csv = Text::CSV->new ({
  binary    => 1,
  auto_diag => 1,
  sep_char  => ','    # not really needed as this is the default
});

my $sum = 0;
open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n";
while (my $fields = $csv->getline( $data )) {
  $sum += $fields->[2];
}
if (not $csv->eof) {
  $csv->error_diag();
}
close $data;
print "$sum\n";