Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: how to merge hash deeply ?

by LanX (Canon)
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 making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2015-07-28 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls