Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Merge 2 hashes which contains duplicate Keys

by davido (Archbishop)
on Sep 25, 2012 at 23:01 UTC ( #995646=note: print w/replies, xml ) Need Help??

in reply to Merge 2 hashes which contains duplicate Keys

It's been done.

use strict; use warnings; use Hash::Merge; use Data::Dumper; my %hash1 = ( this => 1, that => 2, the => 3, other => 4 ); my %hash2 = ( those => 1, them => 2, thine => 3, other => 42 ); my %merged = %{ Hash::Merge->new('RETAINMENT_PRECEDENT')->merge( \%has +h1, \%hash2 ) }; print Dumper \%merged;

Hash::Merge can merge arbitrarily deep hashes, following pre-defined rules for key conflict resolution.


Replies are listed 'Best First'.
Re^2: Merge 2 hashes which contains duplicate Keys
by slayedbylucifer (Scribe) on Sep 26, 2012 at 04:43 UTC
    Thanks Dave. yes, I had gone through the Hash::Merge and it is a life savior. I want to learn more about perl data structure and hence trying things on my own without taking help from any module. Thanks for your time.
Re^2: Merge 2 hashes which contains duplicate Keys
by DavidH (Novice) on May 25, 2015 at 00:41 UTC
    Thanks from one Dave to another, saved me HEAPS of time, what an awesome module!

      You are welcome. :) Makes me happy to hear something I wrote several years ago ended up being helpful later on.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995646]
[Corion]: erix: Yes, but that's mostly for skipping characters you'll never read, but this approach skips the problem of needing to load more data while looking at a half-match
[Corion]: I mostly wonder how I can add this "reversal" to my toolchest, and if it's worth it or just clever
[choroba]: It should greatly simplify the parsing code, no flags or additional reading of the next buffer needed
[Corion]: choroba: Yeah, that's why I like that approach; it seems even quite obvious in retrospect to me, but many things are in hindsight ;)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2016-12-06 15:36 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (109 votes). Check out past polls.