Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
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 exploiting the Monastery: (7)
As of 2014-11-28 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls