FAQ 4.56 How do I merge two hashes?

Discussion in 'Perl Misc' started by PerlFAQ Server, Mar 9, 2011.

  1. This is an excerpt from the latest version perlfaq4.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    4.56: How do I merge two hashes?

    (contributed by brian d foy)

    Before you decide to merge two hashes, you have to decide what to do if
    both hashes contain keys that are the same and if you want to leave the
    original hashes as they were.

    If you want to preserve the original hashes, copy one hash (%hash1) to a
    new hash (%new_hash), then add the keys from the other hash (%hash2 to
    the new hash. Checking that the key already exists in %new_hash gives
    you a chance to decide what to do with the duplicates:

    my %new_hash = %hash1; # make a copy; leave %hash1 alone

    foreach my $key2 ( keys %hash2 )
    {
    if( exists $new_hash{$key2} )
    {
    warn "Key [$key2] is in both hashes!";
    # handle the duplicate (perhaps only warning)
    ...
    next;
    }
    else
    {
    $new_hash{$key2} = $hash2{$key2};
    }
    }

    If you don't want to create a new hash, you can still use this looping
    technique; just change the %new_hash to %hash1.

    foreach my $key2 ( keys %hash2 )
    {
    if( exists $hash1{$key2} )
    {
    warn "Key [$key2] is in both hashes!";
    # handle the duplicate (perhaps only warning)
    ...
    next;
    }
    else
    {
    $hash1{$key2} = $hash2{$key2};
    }
    }

    If you don't care that one hash overwrites keys and values from the
    other, you could just use a hash slice to add one hash to another. In
    this case, values from %hash2 replace values from %hash1 when they have
    keys in common:

    @hash1{ keys %hash2 } = values %hash2;



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
     
    PerlFAQ Server, Mar 9, 2011
    #1
    1. Advertising

  2. PerlFAQ Server

    C.DeRykus Guest

    On Mar 9, 9:00 am, PerlFAQ Server <> wrote:
    > This is an excerpt from the latest version perlfaq4.pod, which
    > comes with the standard Perl distribution. These postings aim to
    > reduce the number of repeated questions as well as allow the community
    > to review and update the answers. The latest version of the complete
    > perlfaq is athttp://faq.perl.org.
    >
    > --------------------------------------------------------------------
    >
    > 4.56: How do I merge two hashes?
    >
    >     (contributed by brian d foy)
    >
    >     Before you decide to merge two hashes, you have to decide what todo if
    >     both hashes contain keys that are the same and if you want to leave the
    >     original hashes as they were.
    >
    >     If you want to preserve the original hashes, copy one hash (%hash1) to a
    >     new hash (%new_hash), then add the keys from the other hash (%hash2 to
    >     the new hash. Checking that the key already exists in %new_hash gives
    >     you a chance to decide what to do with the duplicates:
    >
    >             my %new_hash = %hash1; # make a copy; leave %hash1 alone
    >
    >             foreach my $key2 ( keys %hash2 )
    >                     {
    >                     if( exists $new_hash{$key2} )
    >                             {
    >                             warn "Key [$key2]is in both hashes!";
    >                             # handle the duplicate (perhaps only warning)
    >                             ...
    >                             next;
    >                             }
    >                     else
    >                             {
    >                             $new_hash{$key2} = $hash2{$key2};
    >                             }
    >                     }
    >
    >     If you don't want to create a new hash, you can still use this looping
    >     technique; just change the %new_hash to %hash1.
    >
    >             foreach my $key2 ( keys %hash2 )
    >                     {
    >                     if( exists $hash1{$key2} )
    >                             {
    >                             warn "Key [$key2]is in both hashes!";
    >                             # handle the duplicate (perhaps only warning)
    >                             ...
    >                             next;
    >                             }
    >                     else
    >                             {
    >                             $hash1{$key2} =$hash2{$key2};
    >                             }
    >                     }
    >
    >     If you don't care that one hash overwrites keys and values from the
    >     other, you could just use a hash slice to add one hash to another.. In
    >     this case, values from %hash2 replace values from %hash1 when they have
    >     keys in common:
    >
    >             @hash1{ keys %hash2 } = values %hash2;


    Or even shorter:

    %hash1 = ( %hash1, %hash2 );

    --
    Charles DeRykus
     
    C.DeRykus, Mar 9, 2011
    #2
    1. Advertising

  3. PerlFAQ Server

    C.DeRykus Guest

    On Mar 9, 12:33 pm, "C.DeRykus" <> wrote:
    > On Mar 9, 9:00 am, PerlFAQ Server <> wrote:
    >
    >
    >
    > > This is an excerpt from the latest version perlfaq4.pod, which
    > > comes with the standard Perl distribution. These postings aim to
    > > reduce the number of repeated questions as well as allow the community
    > > to review and update the answers. The latest version of the complete
    > > perlfaq is athttp://faq.perl.org.

    >
    > > --------------------------------------------------------------------

    >
    > > 4.56: How do I merge two hashes?

    >
    > >     (contributed by brian d foy)

    >
    > >     Before you decide to merge two hashes, you have to decide what to do if
    > >     both hashes contain keys that are the same and if you want to leave the
    > >     original hashes as they were.

    >
    > >     If you want to preserve the original hashes, copy one hash (%hash1) to a
    > >     new hash (%new_hash), then add the keys from the other hash (%hash2 to
    > >     the new hash. Checking that the key already exists in %new_hashgives
    > >     you a chance to decide what to do with the duplicates:

    >
    > >             my %new_hash = %hash1; # make a copy; leave %hash1 alone

    >
    > >             foreach my $key2 ( keys %hash2 )
    > >                     {
    > >                     if( exists $new_hash{$key2} )
    > >                             {
    > >                             warn "Key [$key2] is in both hashes!";
    > >                             # handle the duplicate (perhaps only warning)
    > >                             ...
    > >                             next;
    > >                             }
    > >                     else
    > >                             {
    > >                             $new_hash{$key2} = $hash2{$key2};
    > >                             }
    > >                     }

    >
    > >     If you don't want to create a new hash, you can still use this looping
    > >     technique; just change the %new_hash to %hash1.

    >
    > >             foreach my $key2 ( keys %hash2 )
    > >                     {
    > >                     if( exists $hash1{$key2} )
    > >                             {
    > >                             warn "Key [$key2] is in both hashes!";
    > >                             # handle the duplicate (perhaps only warning)
    > >                             ...
    > >                             next;
    > >                             }
    > >                     else
    > >                             {
    > >                             $hash1{$key2} = $hash2{$key2};
    > >                             }
    > >                     }

    >
    > >     If you don't care that one hash overwrites keys and values fromthe
    > >     other, you could just use a hash slice to add one hash to another. In
    > >     this case, values from %hash2 replace values from %hash1 when they have
    > >     keys in common:

    >
    > >             @hash1{ keys %hash2 } = values %hash2;

    >
    > Or even shorter:
    >
    >             %hash1 = ( %hash1, %hash2 );
    >


    efficiency may be an issue though..

    --
    Charles DeRykus
     
    C.DeRykus, Mar 9, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nntp
    Replies:
    9
    Views:
    235
    Anno Siegel
    Oct 12, 2004
  2. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    235
  3. PerlFAQ Server
    Replies:
    0
    Views:
    128
    PerlFAQ Server
    Jan 6, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    112
    PerlFAQ Server
    Feb 26, 2011
  5. PerlFAQ Server
    Replies:
    2
    Views:
    762
    Dr.Ruud
    Apr 22, 2011
Loading...

Share This Page