我使用Text::CSV::Slurp
從一個哈希數組創建一個CSV文件。除了某些標題丟失以外,它工作得很好。使用Perl模塊丟失標題Text :: CSV :: Slurp
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);
}
}
也許你應該顯示你的代碼。 – TLP