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

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

Replies are listed 'Best First'.
Re^6: Merge hashes in specific format
by ovedpo15 (Pilgrim) on Jan 12, 2019 at 23:05 UTC
    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.
      That's wrong. You are clearly declaring it inside the loop.

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

        Why that is wrong? The full sub looks as following:
        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; } );
        Is it impossible to somehow ignore that warning? In other words, make it copy $data{total} for the first iteration?