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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to