packing an array of hashes

[email protected] (Chris Cosner)
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>
I've re-read perlref and have been trying to tease an answer out of the 
Perl Cookbook.

If you put a hash reference into an array
     push @array, \%hash;
you do not store any actual hash data in the array.
So if you change the hash, then later pull the hash reference from the 
array and access it, you get changed data.
If you do this in a loop, you store an array of references to the exact 
same hash, rather than lots of different hashes.
At least, this seems to be what's happening to me in the pseudo code below:

Question: Is there an efficient way (resembling push @array, \%hash) to 
do this that will work? Or do I need to 'unpack' the hash into key => 
value notation to truly add the hash as an element in an array?

while ( condition ) {
     (add data to %address)
     push @addresses, \%address;
     # print statement here shows the hash is different each time 
through the loop
}

for (@addresses){
     %h = %{$_};
     for (keys %h) {
         print "$_ = $h{$_}\n";
        # print statement here prints the same hash over and over
     }


Thanks,

-Chris
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.