Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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
Replies are listed 'Best First'.
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 Athanasius (Abbot) on Jan 29, 2013 at 04:10 UTC
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 chilling in the Monastery: (8)
As of 2015-07-31 10:28 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 (276 votes), past polls