Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Combining two hashes

by Anonymous Monk
on May 17, 2005 at 22:35 UTC ( [id://458025]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a simple, readable way to join two hashes? I'd like to take %my_hash and %my_second_hash and combine them into %my_third_hash, which contains all keys and values from the first two hashes. Alternatively, I'd like to be able to add the key/value pairs from %my_second_hash to %my_hash. I don't really care what happens to duplicate keys as long as I know what will happen.
Thanks!

Replies are listed 'Best First'.
Re: Combining two hashes
by Roy Johnson (Monsignor) on May 17, 2005 at 22:40 UTC

      While this has been shown to be less than optimal in benchmarks on other nodes, it is the simplest to implement. However, it should be noted that (as far as I am aware) the behavior regarding the merger of duplicate hash keys is undefined. On my computer it happens to be right-associative:

      my %a = ('c'=>1,'d'=>2); my %b = ('d'=>1,'e'=>2); my %ab = (%a, %b); # 'd' => 1 my %ba = (%b, %a); # 'd' => 2

      If you need to know (or specify) the behavior regarding duplicate keys as mentioned in the OP, you'll probably need a map in conjunction with one of the LIST context examples below.

      UPDATE: For example, to create a LIST of repeated key values:

      my %a = ('c'=>1,'d'=>2); my %b = ('d'=>1,'e'=>2); my @b{keys %a} = map { exists $b{$_} ? [$b{$_}, $a{$_}] : $a{$_} } key +s %a; # 'd' => [1, 2]

Re: Combining two hashes
by revdiablo (Prior) on May 17, 2005 at 22:45 UTC

    Another way:

    @my_hash{keys %my_second_hash} = values %my_second_hash;

    The keys from %my_second_hash will clobber any duplicates that are in %my_hash.

      My very rudimentary non-conclusive damned lies recommend that the version you showed here is a fair bit faster than either creating the third hash or the %my_hash = ( %my_hash, %my_second_hash ); variant.

      -- Douglas Hunter
Re: Combining two hashes
by monkfan (Curate) on May 18, 2005 at 03:08 UTC
    Hi,
    Just in case, if in each of the two hashes you happen to have two same keys (in example here they are "key1") with different value, and you want to keep them. This could be the way to do it:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my %hash1 = ('key1' => 'val1', 'key2' => 'val9'); my %hash2 = ('key3' => 'val10','key1' => 'val2'); while (my ($key, $val) = each %hash1) { $hash1{$key} = [$val]; } while (my ($key, $val) = each %hash2) { push @{$hash1{$key}}, $hash2{$key}; } print Dumper \%hash1;
    Prints:
    $VAR1 = { 'key2' => [ 'val9' ], 'key1' => [ 'val1', 'val2' ], 'key3' => [ 'val10' ] };
    Regards,
    Edward
Re: Combining two hashes
by displeaser (Hermit) on May 18, 2005 at 07:59 UTC
    Hi,

    CPAN also has a module called Hash:merge which to quote it's POD "Hash::Merge - Merges arbitrarily deep hashes into a single hash".

    Also if you have the perl cookbook see recipe 5.10 on Merging Hashes (if you dont the book, it would definitly be a good one to get)

    A quick search of supersearch also throws up this node: Merging Arrays without duplicates.

    Hope this helps.
    D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-16 06:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found