Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

how to merge hash deeply ?

by chinaxing (Acolyte)
on Jan 29, 2013 at 04:00 UTC ( #1015794=perlquestion: print w/ replies, xml ) Need Help??
chinaxing has asked for the wisdom of the Perl Monks concerning the following question:

I have two configure file defined by perl hash:
#file1: read as $conf_file1 { db => { name => 'pdte', host => 'localhost', } } #file2: read as $conf_file2 { db => { port => '5432', user => 'pdte_usr', password => 'pdte_213456', } } my $new_conf = { %$conf_file1, %conf_file2 }

but this type of merge can only merge hash level1, cannot merge higher than level1.

How can i complete this merge in perl ?

Comment on how to merge hash deeply ?
Download Code
Re: how to merge hash deeply ?
by Athanasius (Monsignor) on Jan 29, 2013 at 04:10 UTC
Re: how to merge hash deeply ?
by Kenosis (Priest) on Jan 29, 2013 at 04:49 UTC

    One option is to use Hash::Merge:

    use strict; use warnings; use Hash::Merge qw/merge/; use Data::Dumper; #file1: read as $conf_file1 my %hash1 = ( db => { name => 'pdte', host => 'localhost', } ); #file2: read as $conf_file2 my %hash2 = ( db => { port => '5432', user => 'pdte_usr', password => 'pdte_213456', } ); my %mergedHash = %{ merge( \%hash1, \%hash2 ) }; print Dumper \%mergedHash;

    Output:

    $VAR1 = { 'db' => { 'password' => 'pdte_213456', 'user' => 'pdte_usr', 'name' => 'pdte', 'port' => '5432', 'host' => 'localhost' } };
      very thanks ! that's what i was finding

        You're most welcome!

Re: how to merge hash deeply ?
by LanX (Canon) on Jan 29, 2013 at 22:38 UTC
    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
Node Status?
node history
Node Type: perlquestion [id://1015794]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2014-12-27 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls