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.


In reply to Re^2: Generate Array of Hashes WITHOUT References by tel2
in thread Generate Array of Hashes WITHOUT References by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.