使用Text :: CSV :: Slurp将哈希转换为CSV

时间:2015-05-15 17:56:05

标签: perl csv

我有一个Perl哈希,我需要写入JSON和CSV格式。

我的哈希的print Dumper( \%formdata )看起来像这样。

$VAR1 = {
          'SPRequest' => {
                              'xrelease' => '13038',
                              'macaddr' => '47:00:11:22:00:30',
                              'name' => 'localhost',
                              'description' => 'demo'
                            },
          '.submit' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ),
          'class' => 'SPRequest',
          '22406' => {
                       'win.profile' => 'production',
                       'win.os_version' => 'standard',
                       'win.os_part_size' => '1'
                     }
        };

这是我用来生成json文件的代码片段。

my $form_data_file = "/tmp/${hostname}_${macaddr}.json";
open FH, ">$form_data_file" or die "Could not open $form_data_file. :$!\n";
print FH to_json( \%formdata, {pretty=>1} );
close FH;

我可以将我的JSON输出到一个如下所示的文件:

[red@tools-dev1 psong]$ cat /tmp/localhost_47-00-11-22-00-30.json 
{
   "SPRequest" : {
      "xrelease" : "13038",
      "macaddr" : "47:00:11:22:00:30",
      "name" : "localhost",
      "description" : "demo"
   },
   ".submit" : true,
   "class" : "SPRequest",
   "22406" : {
      "win.profile" : "production",
      "win.os_version" : "standard",
      "win.os_part_size" : "1"
   }
}

以下是我用来创建CSV文件的代码:

my $form_data_file_csv = "/tmp/${hostname}_${macaddr}.csv";
# Text::CSV::Slurp wants arrayref of hashref
my $ARoHR = [ \%formdata ];
my $csv = Text::CSV::Slurp->create( input => $ARoHR);
open FH, ">$form_data_file_csv" or die "Could not open $form_data_file_csv. :$!\n";
print FH $csv;
close FH;

但问题是我的CSV文件最终看起来像这样:

[red@tools-dev1 psong]$ cat /tmp/localhost_47-00-11-22-00-30.csv ; echo
.submit,22406,SPRequest,class
true,HASH(0x8d81918),HASH(0x8d67980),SPRequest

我在这里做错了什么?

更新:看起来我错误的做法是期待Text::CSV::Slurp使用Hash-of-Hash。所以我自己动手:

my @cols;
my @row;
sub hash2cvs {
        my $h = shift;
        my $p = shift || 'top';
        foreach my $k ( keys %{$h} ) {
                if (ref $h->{$k} eq ref {}) {
                        hash2cvs( $h->{$k}, $k );
                } else {
                        if ( $p eq 'top' ) {
                                push @cols, $k;
                        } else {
                                push @cols, "$p.$k";
                        }
                        push @row, $h->{$k};
                }
        }
}

1 个答案:

答案 0 :(得分:0)

看起来我做错了,期待Text::CSV::Slurp使用Hash-of-Hash。所以我自己动手:

my @cols;
my @row;
sub hash2cvs {
        my $h = shift;
        my $p = shift || 'top';
        foreach my $k ( keys %{$h} ) {
                if (ref $h->{$k} eq ref {}) {
                        hash2cvs( $h->{$k}, ($p eq 'top') ? $k : "$p.$k" );
                } else {
                        if ( $p eq 'top' ) {
                                push @cols, $k;
                        } else {
                                push @cols, "$p.$k";
                        }
                        push @row, $h->{$k};
                }
        }
}