Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Merging hashes

by gods
on Sep 08, 1999 at 12:36 UTC ( #489=perlmeditation: print w/ replies, xml ) Need Help??

Hashes are lists of key value pairs
and merging %bar into %foo:
 @foo{keys %bar} = values %bar;
is pretty dang snazzy.

Comment on Merging hashes
Download Code
RE: Merging hashes
by root (Scribe) on Oct 13, 1999 at 04:05 UTC
    I liked this meditation.
RE: Merging hashes
by Anonymous Monk on Dec 30, 1999 at 10:13 UTC
    how about just this: %foo = (%foo, %bar); the "(" cats the hashes in array context, making one long array with an even number of elements, then assigns that to a hash. Of course this is memory intensive for large hashes. So this is really not very good after all. I must meditate some more in silence...
RE: Merging hashes
by Anonymous Monk on Jan 02, 2000 at 20:34 UTC

    Perl is very wise. I have not tested, but I assume that in the case of %foo = (%foo, %bar); that the values of %bar that share keys with %foo will over write the value in the cat and the value in the new %foo will be from %bar?

    Sorry that sentence was so long, I got caught up in the moment. I use this, not only to garuntee %bar will over write %foo, but so anyone who looks at the code after me will have no dougts as to what I was attempting:

    foreach my $key (keys(%bar)){ $foo{$key} = $bar{$key}; }

    It is iterative, but shouldn't consume too much memory. This will even pass pointers kept in values. I'm afraid a cat would expand the array I was pointing to aswell. I have never tried though.

    -T

RE: Merging hashes
by Anonymous Monk on Apr 07, 2000 at 08:40 UTC
    Este es... the usual mesh.
Re: Merging hashes
by Anonymous Monk on Oct 05, 2013 at 16:30 UTC
    What about something deeper? A deeper meditation? For example a hash containing hashes where the second hash, %bar, takes presidence
      What about it?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://489]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2014-09-22 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls