Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
more useful options
 
PerlMonks  

Re: how to merge hash deeply ?

by LanX (Abbot)
on Jan 29, 2013 at 22:38 UTC ( #1015937=note: print w/ replies, xml ) Need Help??


in reply to how to merge hash deeply ?

For completeness:

if it's only the key db which matters

DB<131> $conf3 = { db => { %{$conf1->{db}}, %{$conf2->{db}} } } => { db => { host => "localhost", name => "pdte", password => "pdte_213456", port => 5432, user => "pdte_usr", }, }

and for the case that you have multiple keys which can also be missing in one of the hashes

DB<132> $conf3 = { map { $_ => { %{$conf1->{$_}}, %{$conf2->{$_}} } +} keys %{{ %$conf1, %$conf2 }} } => { db => { host => "localhost", name => "pdte", password => "pdte_213456", port => 5432, user => "pdte_usr", }, }

Cheers Rolf

UPDATE

maybe easier to understand?

DB<142> sub merge_hr { return { %{$_[0]}, %{$_[1]} } } DB<143> $conf3 = { map { $_ => merge_hr( $conf1->{$_}, $conf2->{$_} +) } keys %{merge_hr($conf1,$conf2)} } => { db => { host => "localhost", name => "pdte", password => "pdte_213456", port => 5432, user => "pdte_usr", }, }


Comment on Re: how to merge hash deeply ?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-04-16 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (436 votes), past polls