Perl
Perl

Reputation: 13

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. 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

Upvotes: 0

Views: 54

Answers (1)

Sobrique
Sobrique

Reputation: 53498

Use Text::CSV - you can either use print if you've got an array reference to 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.

It looks like you're not worried about RollNumber so you can cheat and do:

foreach my $student ( values %student_hash ) { 

}

To iterate. I'm unclear what you're trying to do with address. But a simple join would probably do the trick there. (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 );
}

Upvotes: 1

Related Questions