Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^12: Merge hashes in specific format

by ovedpo15 (Pilgrim)
on Jan 13, 2019 at 00:33 UTC ( [id://1228446]=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^13: Merge hashes in specific format
by tybalt89 (Monsignor) on Jan 13, 2019 at 00:41 UTC

    That's not a sub.

    Posting incomplete code will cause an incomplete an

      > 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

      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; } ); } }
Re^13: Merge hashes in specific format
by pryrt (Abbot) on Jan 13, 2019 at 00:44 UTC

    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.
        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.

        For reasons of practicality that doesn't matter. When you post code (which should be an SSCCE but in your case is not) which contains such mistakes and they are pointed out to you and you ignore them it does not encourage anyone else to try to help you. If you haven't even been bothered to fix the bugs which you can then why do you expect anyone to help you fix the bugs which you cannot?

        I do understand the problem, I know why it occurs and where I just don't understand how to solve it.

        Then why isn't that the code you have shown, as an SSCCE so that it is then obvious where the problem lies? Take a moment to read that link and understand that all the monks have only seen a little bit of your code which, in isolation and being incomplete, makes it very difficult to debug. Read also the excellent How to ask better questions using Test::More and sample data to improve the problem descriptions even further.

        Anyway, I feel like you and especially LanX, are not so nice.

        And there's a perfect way to alienate a community of experienced Perl hackers who would otherwise give you the benefit of their wisdom for free. Recant, brother, while you yet may.

        Sorry, didn't intend to be "not so nice". When debugging problems remotely, and aren't intimately acquainted with the goal, people in help forums often focus on the first issues we see, even if they aren't the root cause. Moreover, it seemed reasonable that that it was your root cause (though it turned out not to be) because the new as-of-yet unassigned $files_href created by the second 'my' would have started as undefined, so it appeared that was why it was trying to use an "undefined value as hash reference". I saw what LanX was hinting about, and agreed that it was a good first step in the debugging process for you; since your post indicated you hadn't seen the 'my' issue yet, I tried to clarify, to further help you along toward fixing that part of the issue.

        I guess "staring you in the face" might have been harsh -- I didn't intend it to be. Sorry

        It's certainly not funny for us.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-25 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found