Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 http://perldoc.perl.org/perlreftut.html 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 +lice_hash.pl 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.

Thanks.


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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (5)
    As of 2014-04-20 09:16 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (485 votes), past polls