Just to expand a bit on what Roy Johnson said. In your first statement inside the while loop, you assign a brand new hash to $$patients{$row->{'pat_id'}} each time through the loop. Thus, what you probably mean to do is to assign only if it hasn't already been set:

while (my $row = $sth->fetchrow_hashref) { $$patients{$row->{'pat_id'}} ||= { first_name => "$row->{'first_name'}", last_name => "$row->{'last_name'}", pid => "$row->{'pat_id'}" }; push(@{$$patients{$row->{'pat_id'}}->{files}}, $row->{'file_name +'}); $x++; }
All I did was add two characters: "||". What that says is to do the assignment if and only if the variable is currently false. Which it would be if not yet set. The first time through the loop for the current pat_id, it will get set to a hash ref which should always be true (if it isn't, it's because we ran out of memory, which is a bigger problem than this anyway), which means we won't assign to it a second time. Try doing a Super Search for "orcish" to see other examples of the OR-Cache (pronounced almost like "orcish") style, which is basically what I'm suggesting here.


In reply to Re: Push into Hash of Array by Tanktalus
in thread Push into Hash of Array by bkiahg

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.