[英]Perl 2D Array with undef to CSV
I have to change a 2D array with undef enteries into a csv file. 我必须将带有undef enteries的2D数组更改为csv文件。 The dumper values looks like
自卸车值看起来像
VAR1 [
'Entry1',
undef,
undef,
undef,
17
],
[
'Entry2',
8,
undef,
undef,
17
],
[
'Entry3',
undef,
4,
undef,
17
],
For undef, I need to put 0. I am using the code below, but its not giving required results. 对于undef,我需要输入0。我正在使用下面的代码,但未提供所需的结果。
open MYFILE, ">>new.csv";
for ($i=1,$i <= $flag, $i++){
print MYFILE "$outputArray[$i][0],";
for ($j=0,$j<= 4, $j++){
print MYFILE int($outputArray[$i][$j]).",";
}
print MYFILE "\n";
}
close MYFILE;
My final csv would be something like 我最终的csv将是这样的
Entry1,0,0,0,17
Entry2,8,0,0,17
Entry3,0,4,0,17
Use the foreach
style of for
when iterating over a multidimensional array. 在多维数组上
for
迭代时,请使用for
的foreach
样式。 There's no reason to keep track of indexes if you don't have to. 如果没有必要,没有理由跟踪索引。
Additionally, you can use the defined or operator //
with map
to quickly translate all your undef
values to your preferred default. 此外,您可以使用带有
map
的已定义或运算符//
将所有undef
值快速转换为首选默认值。
for my $row (@outputArray) {
print MYFILE join(',', map {$_ // 0} @$row), "\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.