in reply to Push into Hash of Array
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:
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.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++; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Push into Hash of Array
by bkiahg (Pilgrim) on Oct 25, 2005 at 20:14 UTC |