idy has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have 2 hash with common key. I need to join the value of hash1 and value of hash2 and put it into either same hash or different hash.

map1 843272-->| |44341| | |S|OCEAN|BLVD| |UNIT 8B| | |N MYRTLE BCH|SC|XXXXXX

map2 843272-->| |253451| | |S|OCEAN|BLVD| |UNIT 508| | |N MYRTLE BCH|SC|XXXXX

It's in the format key --> value
Key can be same or different.How to join the value when keys are same ? Can anyone help me with a code for this ?

Replies are listed 'Best First'.
Re: join value of hash
by MidLifeXis (Monsignor) on Nov 10, 2009 at 17:12 UTC

    How about a Hash of Arrays (HoA)?

    my $hash = { 843272 => [ '| |44341| | |S|OCEAN|BLVD| |UNIT 8B| | |N MYRTLE BCH|SC|XXXXX +X', '| |253451| | |S|OCEAN|BLVD| |UNIT 508| | |N MYRTLE BCH|SC|XXX +XX ', ], };

    See push, perlref, perldsc, and perllol for more documentation.

    --MidLifeXis

Re: join value of hash
by ikegami (Patriarch) on Nov 10, 2009 at 17:13 UTC
    for my $k (keys(%hash1)) { next if !exists($hash2{$k}); my $v = join_values( delete($hash1{$k}), delete($hash2{$k}) ); print "$k-->$v\n"; } print("Leftovers in hash 1:\n"); print "$_\n" for keys(%hash1); print("Leftovers in hash 2:\n"); print "$_\n" for keys(%hash2);
Re: join value of hash
by eff_i_g (Curate) on Nov 10, 2009 at 17:14 UTC
    What do you mean by "join"? Do you want to concatenate the values? Perhaps turn the hash value into an array reference and store both separately?