pavan
pavan

Reputation: 314

cant get array of hashes in perl

I have the Employees CSV Data and i try to insert each employee hash in to an array

open($empOutFh,">empOut.txt")
    $hash= [];
    while(<$empFh>) {
        @columnNames = split /,/, $_ if $.==1;
        @columnValues = split /,/, $_;
        %row = map{$_=>shift @columnValues}@columnNames;
        push @$hash,\%row;
    } 
    print Dumper($hash);

I am getting the output has

$VAR1 = [
          {
            'emp_no' => '11000',
            'hire_date
' => '1988-08-20
',
            'birth_date' => '1960-09-12',
            'gender' => 'M',
            'last_name' => 'Bonifati',
            'first_name' => 'Alain'
          },
          $VAR1->[0],
          $VAR1->[0],
          $VAR1->[0]
      ]

But when i am try to print each row it showing different row hash for each time

Upvotes: 0

Views: 90

Answers (2)

shawnhcorey
shawnhcorey

Reputation: 3601

If you can't get a map to work properly, use a foreach loop instead. Being able to maintain the code is more important than being clever.

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------
use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# --------------------------------------

# open($empOutFh,">empOut.txt")
my $emp_file = 'empOut.txt';
open my $emp_out_fh, '>', $emp_file or die "could not open $emp_file: $!\n";

#     $hash= [];
my @emps = ();
my @columnNames = ();

#     while(<$empFh>) {
while( my $line = <$empFh> ){
    chomp;

#         @columnNames = split /,/, $_ if $.==1;
    if( $. == 1 ){
        @columnNames = split /,/, $line;
        next;
    }

#         @columnValues = split /,/, $_;
    my @columnValues = split /,/, $line;
    my %row = ();

#         %row = map{$_=>shift @columnValues}@columnNames;
    for my $i ( 0 .. $#columnNames ){
        $row{$columnNames[$i]} = $columnValues[$i];
    }

#         push @$hash,\%row;
    push @emps, \%row;

#     } 
}

#     print Dumper($hash);
print Dumper \@emps;

Upvotes: 0

ruakh
ruakh

Reputation: 183456

The problem is that you're using a single hash %row, so \%row is always referring to the same hash. Every time you assign to %row, you're not setting it to a new hash, you're just clearing out the same hash and repopulating it (thereby affecting, indirectly, every element of your array).

To fix this, you need to create a new hash in each loop iteration. The minimal change to your code would be to declare %row as a lexical variable with local scope, by using the my operator:

        my %row = map { $_ => shift @columnValues } @columnNames;
        push @$hash, \%row;

Another option is to eliminate the intermediate variable entirely, and just generate a reference to a new anonymous hash on each pass:

        push @$hash, { map { $_ => shift @columnValues } @columnNames };

Upvotes: 3

Related Questions