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

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Merge hashes in specific format
by ovedpo15 (Pilgrim) on Jan 12, 2019 at 22:30 UTC
    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}
      Declare final_href outside the loop and initialize there empty data and total hashes as defaults.

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

        Thank you for the reply. final_href is an empty hash ref that I pass to this subroutine in order to insert the data into it.
        I was wondering if there is a way to somehow make it work without adding data into that hash.
        Thank you.