davorg is correct. You are reusing the same hash each time through the loop. Your debug statement printed the correct values in the hash each time, but what you didn't see is that the hashref itself was the same, as the following example shows. (Note: I changed the while loop to a for loop.)

use strict; use warnings; { print "This is the original approach, reusing \$attrHash\n"; my $result = []; my $attrHash = {}; for( 1..5 ) { print $attrHash, "\n"; push( @$result, $attrHash ); } } { print "This uses a new hash each time through the loop\n"; my $result = []; for( 1..5 ) { my $attrHash = {}; print $attrHash, "\n"; push( @$result, $attrHash ); } } __END__ This is the original approach, reusing $attrHash HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) HASH(0x2254c8) This uses a new hash each time through the loop HASH(0x225504) HASH(0x225534) HASH(0x225324) HASH(0x2254e0) HASH(0x183c1d0)

If you declare $attrHash within the loop you'll get a new reference each time.


In reply to Re: Problem pushing hashref results by bobf
in thread Problem pushing hashref results by rashley

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.