in reply to Re: Generate Array of Hashes WITHOUT References
in thread Generate Array of Hashes WITHOUT References
> Fascinating how most replyers misunderstood you're question, cause they read the title but not the code.
I didn't notice. Which posts are you refering to?
> This is wrong
> $rec=();
> You need to write %rec=() because you don't want references.
Sorry - typo on my part (that is what I meant to type). However, but after making that change in my code below:
...which includes the change of push'ing \%rec, I get this output:#!/usr/bin/perl use Data::Dumper; use warnings; while ( <DATA> ) { #$rec = {}; #$rec = (); %rec = (); for $field ( split ) { ($key, $value) = split /=/, $field; $rec{$key} = $value; } push @AoH, \%rec; } print Dumper(\@AoH); __DATA__ A=1 B=2 C=3 Y=4 Z=5
Which is wrong, but if I change "%rec = ();" to "my %rec;" (as per Anonymous Monk's solution), I get what I want:$VAR1 = [ { 'Z' => '5', 'Y' => '4' }, $VAR1->[0] ];
Any ideas why? What's the significant difference between "%rec = ();" and "my %rec;" in this case?$VAR1 = [ { 'A' => '1', 'C' => '3', 'B' => '2' }, { 'Z' => '5', 'Y' => '4' } ];
> WHY don't you use warnings ??
No good reason (force of (bad) habit), but apart from the reason you mentioned in a later post, having tried "use warnings" in my code now (as above), it looks as if it would not have reported any problem. "use strict" may have helped.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Generate Array of Hashes WITHOUT References
by parv (Parson) on Sep 10, 2014 at 14:34 UTC | |
by tel2 (Pilgrim) on Sep 10, 2014 at 23:00 UTC | |
by Anonymous Monk on Sep 11, 2014 at 07:11 UTC |