Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: how to merge hash deeply ?

by LanX (Saint)
on Jan 29, 2013 at 22:38 UTC ( [id://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", }, }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found