lonewolf28 has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, I have file which contains name, payment and date. I'm trying to extract the data and store in an array as a hash reference.
sub main { my $file = 'new_text.csv'; open FH, '<', $file or die "Cannot open the $file: $!"; <FH>; my %hash; my @array_of_hash; LINE: while(my $line = <FH>) { chomp $line; $line =~ s/\s+\$|approx\.\s*|\?|^\s*//g; next LINE if ( $line !~ /\S+/ ); my @values = split /\s*,\s*/, $line; if ( scalar @values != 3){ next LINE; } foreach ( @values ){ if ( $_ eq '' ) { next LINE; } } my ($name, $payment, $date ) = @values; %hash = ( 'name' => $name, 'payment' => $payment, 'date' => $date, ); push @array_of_hash, \%hash; } } main();
#I'm expecting an output like { name => xxx, payment => xxx, date => xxx, }, { name => xxx, payment => xxx, date => xxx, }, { name => xxx, payment => xxx, date => xxx, }, { name => xxx, payment => xxx, date => xxx, }, #Unfortunately i'm getting an output like... print Dumper (@array_of_hash); $VAR1 = { 'payment' => '0.57', 'name' => 'Rene Descarted', 'date' => '10072033' }; $VAR2 = $VAR1; $VAR3 = $VAR1; $VAR4 = $VAR1;
I'm not sure what silly mistake i'm making here. Thank You.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Array of hashes not working as expected
by toolic (Bishop) on Feb 25, 2015 at 18:13 UTC | |
|
Re^2: Array of hashes not working as expected
by fishmonger (Chaplain) on Feb 25, 2015 at 18:21 UTC | |
|
Re^2: Array of hashes not working as expected
by hdb (Monsignor) on Feb 25, 2015 at 20:20 UTC |