in reply to Merge hashes in specific format

#!/usr/bin/perl # https://perlmonks.org/?node_id=1228412 use strict; use warnings; my $one = { data => { dir1 => { fileA => { pid => { 61781 => 1 }, total => 13 } +, fileB => { pid => { 61799 => 1 }, total => 12 } +, }, dir2 => { fileA => { pid => { 61439 => 1 }, total => 5 }, fileC => { pid => { 12345 => 1 }, total => 10 } +, }, }, total => { fileA => 18, fileB => 12, fileC => 10 }, }; my $two = { data => { dir3 => { fileA => { pid => { 616161 => 1 }, total => 6 } +, fileD => { pid => { 54321 => 1 }, total => 12 } +, }, dir4 => { fileA => { pid => { 1718 => 1 }, total => 2 }, fileE => { pid => { 15151 => 1 }, total => 3 }, }, }, total => { fileA => 8, fileD => 12, fileE => 3 }, }; my $newhash = { data => { %{ $one->{data} }, %{ $two->{data} } }, total => do { my %total; for my $href ( $one, $two ) { $total{$_} += $href->{total}{$_} for keys %{ $href->{total} }; } \%total; } }; use Data::Dump 'dd'; dd $newhash;

Outputs:

{ data => { dir1 => { fileA => { pid => { 61781 => 1 }, total => 13 } +, fileB => { pid => { 61799 => 1 }, total => 12 } +, }, dir2 => { fileA => { pid => { 61439 => 1 }, total => 5 }, fileC => { pid => { 12345 => 1 }, total => 10 } +, }, dir3 => { fileA => { pid => { 616161 => 1 }, total => 6 } +, fileD => { pid => { 54321 => 1 }, total => 12 } +, }, dir4 => { fileA => { pid => { 1718 => 1 }, total => 2 }, fileE => { pid => { 15151 => 1 }, total => 3 }, }, }, total => { fileA => 26, fileB => 12, fileC => 10, fileD => 12, fileE + => 3 }, }

Replies are listed 'Best First'.
Re^2: Merge hashes in specific format
by Veltro (Hermit) on Jan 13, 2019 at 18:50 UTC

    Hi tybalt89,

    Sorry to post this so late but I couldn't find time to do this earlier. I have a question regards your technique to merge and the one that I posted.

    With the method that I posted I specifically did not choose to use Hash::Merge and also specifically not { ( %{ $ ... }, %{ $ ... } ) } but instead to make it LEFT_PRECEDENT on the dir1 .. dirN. (Even though the design criteria said that these elements would be unique). The method that I created would ignore any new 'dir' element that already existed and so it will not corrupt already collected data.

    If I would have used Hash::Merge I would have probably done something like:

    use strict ; use warnings ; use Data::Dumper ; use List::Util qw { sum0 } ; use Hash::Merge ; my $VAR1 = { # Same as before }; my $VAR2 = { # same as before }; my $merger = Hash::Merge->new('RETAINMENT_PRECEDENT'); my $VAR3 = $merger->merge( $VAR1, $VAR2 ) ; foreach(keys %{$VAR3->{ total }}) { if ( ref $VAR3->{ total }->{ $_ } eq 'ARRAY' ) { $VAR3->{ total }->{ $_ } = sum0 @{$VAR3->{ total }->{ $_ }} ; } } print Dumper( $VAR3 ) ;

    When receiving 'dir' elements again and using the technique of { ( %{ $ ... }, %{ $ ... } ) } would corrupt the data:

    • different pid: data loss
    • calculates wrong totals

    My question basically is, which of the three techniques is in your opinion more robust. Even though I directed this question to tybalt89, anyone else feel free to answer as well of course.

      "the design criteria said that these elements would be unique"

      If the specification changes, then I can charge extra to adapt it :)

Re^2: Merge hashes in specific format
by ovedpo15 (Pilgrim) on Jan 12, 2019 at 21:53 UTC
    Great, thank you. How to make $newhash to be a real hash? Isn't it a scalar right now?

      It is a scalar anonymous hash reference. If by "real hash" you mean "not an anonymous hash reference", try (untested):
          my %newhash = ( data => { ... }, total => do { ... }, );


      Give a man a fish:  <%-{-{-{-<

        So I have done the following sub:
        foreach my $dir (@{$dirs}) { decode($dir.".data.json",%data); # will change name my $final_href = { data => { ( %{$final_href->{data} },%{ $data{data} } ) }, total => do { my %total; for my $href ( $final_href->{total}, $data{total} ) { $total{$_} += $href->{total}{$_} for keys %{ $href +->{total} }; } \%total; } };
        But it will fail because at start $final_href->{data} is not defined. How to make it work even if its not defined (in that case I should just get the $data{$data}