简体   繁体   English

如何将数组项哈希值的哈希值发送到csv

[英]How to send hash of hash of array items to csv

Hash of Hash of values were printing correctly to csv but hash of hash of array values are messing up my csv file. 哈希值散列正确地打印到csv,但数组值哈希的散列弄乱了我的csv文件。 Please find my code below which is not working. 请在下面找到我的代码不起作用。

<    use strict;
     use warnings;
     use Data::Dumper;

     my %student_hash;
     my $student_report = "StudentReport.csv";

     %student_hash = (
            'RollNummber1' => {
                               'studentname' => 'Boris',
                               'address' => ['Vietnam',
                                             'local'
                                            ],
                               'DOB' => '5june2000'

                               },
            'RollNummber2' => {
                               'studentname' => 'John',
                               'address' => [ 
                                             '4th/floor',
                                             'Culverdown/Street',
                                             'WestHam',
                                             'UK.',
                                            ],
                               'DOB' => '2feb2000'

                               },
            'RollNummber3' => {
                               'studentname' => 'Karen',
                               'DOB' => '5march2000'

                              }
           );

          print "StudentHash:".Dumper(\%student_hash);

          open(my $fh, '>', $student_report) or die("Couldn't open ile");
          print $fh "DOB \t,ADDRESS \t,NAME \t\n";

          foreach my $key(keys %student_hash){
           foreach my $secondkeys (keys %{$student_hash{$key}}){

             if($secondkeys =~ /DOB || studentname/) {
               print $fh "$student_hash{$key}{$secondkeys} \t,"; } 
             if($secondkeys =~ /address/) {
               print $fh Dumper $student_hash{$key}{$secondkeys} }; 
        }
    print $fh "\n";
 }
 close($fh);>

If I comment out printing the array part then the code works fine.Can someone please suggest how to print the array elements into csv either in one cell or in multiple cells.Thanks 如果我注释掉打印数组部分,那么代码可以正常工作。有人可以建议如何在一个或多个单元格中将数组元素打印到csv中。

Use Text::CSV - you can either use print if you've got an array reference to print. 使用Text::CSV -您可以使用print ,如果你有一个数组引用打印。 (It handles quoting for you). (它为您处理报价)。

Or you can use column_names to set the column ordering, and print_hr to output a hash ref in that order. 或者,您可以使用column_names设置列顺序,并使用print_hr以该顺序输出哈希引用。

It looks like you're not worried about RollNumber so you can cheat and do: 您似乎并不担心RollNumber所以您可以作弊并执行以下操作:

foreach my $student ( values %student_hash ) { 

}

To iterate. 进行迭代。 I'm unclear what you're trying to do with address . 我不清楚您要如何处理address But a simple join would probably do the trick there. 但是简单的join可能会解决问题。 (either on linefeed or whitespace, depending on the desired output). (在换行符或空格上,取决于所需的输出)。

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

my %student_hash = (
    'RollNummber1' => {
        'studentname' => 'Boris',
        'address'     => [ 'Vietnam', 'local' ],
        'DOB'         => '5june2000'

    },
    'RollNummber2' => {
        'studentname' => 'John',
        'address' => [ '4th/floor', 'Culverdown/Street', 'WestHam', 'UK.', ],
        'DOB'     => '2feb2000'

    },
    'RollNummber3' => {
        'studentname' => 'Karen',
        'DOB'         => '5march2000'

    }
);

my $csv = Text::CSV->new( { sep_char => ',', eol => "\n" } );
$csv->column_names( 'studentname', 'DOB', 'address' );
foreach my $student ( values %student_hash ) {
    if ( defined $student -> {address} ) { 
        $student -> {address} = join (" ", @{$student->{address}});
    }

    $csv->print_hr( \*STDOUT, $student );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM