Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Monks,

THis question is regarding merging two hashes with duplicate keys. I found few solutions on hte internet and they all work, but I could not understand any of them . Hence working it out on my own.

Also, I tried the Hash::Merge and it works too, but I am feeling that this is not really that diffuclt and hence want to do it myself.

Lets consider below 2 hashes:

my %h1 = ( "one" => 1, "two" => 2, "three" => 3, ); my %h2 = ( "four" => 4, "five" => 5, "six" => 6, "one" => 2222, );

I want to merge these two hash so that the resulting hash look like below:

$VAR1 = { 'one' => [ 1, 2222 ] 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6, };

So when a duplicate key is found, the value of that key should get into an anonymous array

I referred and wrote below code:

#!/usr/bin/perl -w use strict; use Data::Dumper; my %h1 = ( "one" => 1, "two" => 2, "three" => 3, ); my %h2 = ( "four" => 4, "five" => 5, "six" => 6, "one" => 11111, ); foreach my $x ( keys %h2 ){ push @{ $h1{$x} }, $h2{$x}; } print Dumper (\%h1);

the output is:

Can't use string ("1") as an ARRAY ref while "strict refs" in use at s line 48

So, I remove "use strict" and I get below output:

$VAR1 = { 'three' => 3, 'five' => [ 5 ], 'six' => [ 6 ], 'one' => 1, 'two' => 2, 'four' => [ 4 ] };

I am not able to figure out where I am going wrong. Any suggestions would be of great help.


In reply to Merge 2 hashes which contains duplicate Keys by slayedbylucifer

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2021-01-19 15:31 GMT
    Find Nodes?
      Voting Booth?