我正在使用Text::CSV::Slurp
从散列数组创建 CSV 文件。除了缺少一些标题外,它工作得很好。
hash1
header1 header2
1 2
hash2
header1 header2 header3
11 22 33
我希望最终输出为 CSV 文件:
header1 header2 header3
1 2
11 22 33
不是啜饮输出
header1 header2
1 2
11 22
有什么建议么?
代码:
sub entry_capture_csv {
my ($stores_folder, $cmdstr, $header_field, $entrycmdstr, $entryfilename) = @_;
print "\t\tOSSI-" . $cmdstr . " for " . $entryfilename . " entry detail\n";
$node->pbx_command($cmdstr);
if ( $node->last_command_succeeded() ) {
my @ossi_output = $node->get_ossi_objects();
my $i = 0;
my @ext_array;
foreach my $hash_ref(@ossi_output) {
$i++;
#print "output result $i\n";
for my $field ( sort keys %$hash_ref ) {
my $value = $hash_ref->{$field};
#print "\t$field => $value\n";
}
my $entryNumber = trim($hash_ref->{$header_field});
unless( defined $entryNumber ) { $entryNumber = '' };
if ($entryNumber eq "") {
#empty string
} else {
push(@ext_array, $entryNumber);
}
}
# Issue = failed sometimes there is extra header
#
my @result_array;
foreach (@ext_array) {
my $entrycmd = $entrycmdstr . " " . $_;
$node->pbx_command($entrycmd);
if ( $node->last_command_succeeded() ) {
print "\t\t\t" . $entrycmd . "\n";
my @ossi_output = $node->get_ossi_objects();
push(@result_array, @ossi_output);
#my $csv = Text::CSV::Slurp->create( input => \@ossi_output );
#open (OUTFILE, ">$stores_folder/$store-" . $entryfilename . "-" . "$_" . ".csv") || die "Can't open output file.\n";
#print OUTFILE $csv;
#close(OUTFILE);
} else {
print "Failed\t\t\t" . $entrycmd . "\n";
}
}
my $csv = Text::CSV::Slurp->create( input => \@result_array );
open (OUTFILE, ">$stores_folder/$store-" . $entryfilename . ".csv") || die "Can't open output file.\n";
print OUTFILE $csv;
close(OUTFILE);
}
}