Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Merging hashes

by Master Aalnan (Initiate)
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.

Replies are listed 'Best First'.
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.


RE: Merging hashes
by root (Monk) on Oct 13, 1999 at 04:05 UTC
    I liked this meditation.
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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2023-11-29 19:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found