Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: merge two hashes into one

by sundialsvc4 (Abbot)
on May 29, 2018 at 22:20 UTC ( [id://1215411]=note: print w/replies, xml ) Need Help??


in reply to merge two hashes into one

Some of the offered answers seem wrong to me.   If you want to add an element to the output hash only if the key exists in both inputs, I think that you would need something that is essentially this (tested) logic:

use strict; use warnings; use Data::Dumper; my $h1 = { 'a' => 1, 'b' => 2, 'c' => 3 }; my $h2 = { 'b' => 2, 'c' => 4, 'd' => 5 }; my $hout = {}; foreach my $k (keys $h1) { if (exists $h2->{$k}) { $hout->{$k} = $h1->{$k} + $h2->{$k}; } } print Data::Dumper->Dump([$hout], ['hout']); $hout = { 'b' => 4, 'c' => 7 };
Loop through all of the keys in one hash and check to see if this key also exists in the other.   If so, populate the output hash with the sum of the two.

Looking through the various answers, it seemed to me that some either modified $h1, or assumed that the key existed in $h2, or relied upon side effects.   (Of course I did not down-vote any of them – I never do – and perhaps I am quite mistaken.)

Now, as far as “a good-looking, less-coded way,” the only criteria that really matters to me is that the logic is tested, that it is “at-a-glance obvious to anyone and everyone,” and that it is easy to maintain.   If a future program-change requirement is to do different or additional things to the matching elements, it should be easy to make that modification without breaking what’s there.   I place no premium on brevity – quite the opposite.   (My career experience commonly deals with software that is dozens of years old and built by a great many someone-elses, so I am very keen to such concerns.)

Replies are listed 'Best First'.
Re^2: merge two hashes into one
by hippo (Bishop) on May 29, 2018 at 22:40 UTC

    From the doc for keys:

    Starting with Perl 5.14, an experimental feature allowed keys to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24.

    So when you "tested" this the very best possible result was that you saw the "keys on reference is experimental" warning and blithely ignored it. Otherwise your code didn't compile at all and you ignored that.

      So when you "tested" this ... you saw the "keys on reference is experimental" warning .... Otherwise your code didn't compile at all ...

      I don't know what Perl version sundialsvc4 was using, but I tested the code under Perl 5.14 and it works as advertised.

      I and many others have long had a problem with sundialsvc4: he so often just doesn't seem to know what the hell he's talking about. Even when I occasionally encounter a post of his that seems, at first glance, to make a valid point, I have gotten into the habit of simply passing on by: I've been bitten too often by bugs and gotchas that only become apparent on closer inspection or through the replies of others to risk upvoting.

      Lately, I've seen a couple of his posts that are so clear, straighforward and helpful that I've upvoted them; this is one. Of course, a note about the "highly experimental" nature of  keys EXPR would have been nice and there's still a bit of bloviation, but you can't have everything. There's code, compilable, runnable code that works as described, and a few valid points! sundialsvc4 takes up so much bandwidth on this site that it's a shame his contributions haven't been correspondingly worthwhile. Is a new day dawning? I hope so, and I think any effort in that direction is worth encouraging.


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

        There's code, compilable, runnable code that works as described

        But if, and only if, you happen to be running one of a few select versions of perl which support it. Try compiling with perl newer than 5.22 or older than 5.14 and see how you get on.

        You could call this ignorance or an oversight on the point of sundialsvc4 except that he specifically said, in italics no less, that it was "tested". Which means he ran it, saw the warning and ignored it. He didn't investigate further and he didn't even mention it in the post. That's bad enough but when you consider he's supposedly trying to help a new perl coder - someone who hasn't read the FAQs and is asking about a well-trodden topic - do you still think it is well-intentioned?

        That's a rhetorical question. Having pointed this out again I'll contribute no further to this subthread.

        Yes, AnomalousMonk ++, I fully agree. I have also upvoted Sundial twice in about half a day.
        sure, give him another chance, hes only had 500 of em

        stockholm syndrome?

        hes just testing his latest bs theory that 7 people always downvote him

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-19 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found