Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners Cgi > Re: packing an ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 1411 of 1490
Post > Topic >>

Re: packing an array of hashes

by mritty@[EMAIL PROTECTED] (Paul Lalli) Aug 8, 2007 at 10:26 AM

On Aug 7, 7:47 pm, ccos...@[EMAIL PROTECTED]
 (Chris Cosner) wrote:
> 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 @[EMAIL PROTECTED]
 \%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 @[EMAIL PROTECTED]
 \%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 @[EMAIL PROTECTED]
 \%address;
>      # print statement here shows the hash is different each time
> through the loop
>
> }

Shawn already gave you one answer.  Allow me to point out that if your
comment "(add data to %address)" actually means to reassign the entire
hash, rather than to just add or delete elements from the hash, then
the far more correct solution is to just scope your variables
correctly:

my @[EMAIL PROTECTED]
 (keep_going()) {
    my %address = ( ... => ..., ... => .... );
    push @[EMAIL PROTECTED]
 \%address;
}

instead of:
my @[EMAIL PROTECTED]
 %address;
while (keep_going()) {
    %address = ( ... => ..., ... => .... );
    push @[EMAIL PROTECTED]
 \%address;
}

This way, as soon as one iteration of your loop ends, the %address
that was declared in that iteration goes away, and you get a brand new
hash the next time around.

If, however, you're only modifying an existing hash, then Shawn's
solution is the only correct one:
my @[EMAIL PROTECTED]
 %address;
while (keep_going() ) {
   $address{...} = ....;
   push @[EMAIL PROTECTED]
 { %address };
}

Paul Lalli
 




 5 Posts in Topic:
packing an array of hashes
ccosner@[EMAIL PROTECTED]  2007-08-07 16:47:06 
Re: packing an array of hashes
shawnhcorey@[EMAIL PROTEC  2007-08-07 20:01:12 
Re: packing an array of hashes
ccosner@[EMAIL PROTECTED]  2007-08-07 17:27:11 
Re: packing an array of hashes
rcook@[EMAIL PROTECTED]   2007-08-08 10:01:27 
Re: packing an array of hashes
mritty@[EMAIL PROTECTED]   2007-08-08 10:26:45 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 9:55:31 CDT 2008.