Thanks for your reply, Rolf.
> 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:
#!/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 includes the change of push'ing \%rec, I get this output:
$VAR1 = [
{
'Z' => '5',
'Y' => '4'
},
$VAR1->[0]
];
Which is wrong, but if I change "%rec = ();" to "my %rec;" (as per Anonymous Monk's solution), I get what I want:
$VAR1 = [
{
'A' => '1',
'C' => '3',
'B' => '2'
},
{
'Z' => '5',
'Y' => '4'
}
];
Any ideas why? What's the significant difference between "%rec = ();" and "my %rec;" in this case?
> 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. |