in reply to Re^10: Merge hashes in specific format
in thread Merge hashes in specific format

Are you trying to do something like this?

#!/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 }, }; sub combinemanyhashes { my $newhash = { data => { map %{ $_->{data} // {} }, @_ }, total => do { my %total; for my $href ( @_ ) { $total{$_} += $href->{total}{$_} for keys %{ $href->{total} // + {} }; } \%total; } }; return $newhash; } my $totalhashref = {}; $totalhashref = combinemanyhashes( $totalhashref, $one, $two ); use Data::Dump 'dd'; dd $totalhashref;

Replies are listed 'Best First'.
Re^12: Merge hashes in specific format
by ovedpo15 (Pilgrim) on Jan 13, 2019 at 00:33 UTC
    Not quite but thank you for the reply. I have created the following sub:
    my ($name,$dirs,$final_href) = @_; my %data; foreach my $dir (@{$dirs}) { decode($dir."/".$name,%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 won't work because when I pass $final_href to this sub, it is empty, so it will fail "Can't use an undefined value as a HASH reference"
    . I tried to add:
    $files_href->{data} = (); $files_href->{total} = ();
    But it also does not work. it wants a real hash (because we do dereferencing with %{$final_href->{data}).
    Any ideas how to solve it?

      That's not a sub.

      Posting incomplete code will cause an incomplete an

        I'm sorry. here you go:
        my %final_href; merge_jsons(".data.json",\@dirs,\%final_href); sub merge_jsons { my ($name,$dirs,$final_href) = @_; my %data; foreach my $dir (@{$dirs}) { decode($dir."/".$name,%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->{to +tal} }; } \%total; } ); } }
        > Posting incomplete code will cause an incomplete an

        brilliant! xD

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      In the code you've posted -- now twice -- the answer is staring you in the face. LanX was trying to help you learn how to debug your own code by giving hints, and asking you to research declarations. Since that apparently wasn't enough, my advice to you (HINT: note the link I just embedded) is to look at lines 1 and 7 in the code I am replying to, and maybe study more about "perl scope" as well.

        If you are talking about the 'my' before the $final_href on line 7, it was by mistake, but it is NOT the issue I was facing. Even if I'll remove 'my' (and that is true that I have to do it), it WON'T fix the problem because that problem is in the "%{$files_href->{data}". I do understand the problem, I know why it occurs and where I just don't understand how to solve it. Actually, I do know how to solve it but it does not feels right (I can use some empty hash and insert it into the 'data' and 'total' before the loop). The problem is that at the first iteration "$files_href->{data}" is not defined and my code tries to do "%" on a non-defined 'value'.

        Anyway, I feel like you and especially LanX, are not so nice. Maybe to you, the problem is so easy that its funny, but for me, it does not. I'm sorry for wasting your time.