Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   FAQ 4.56 How do I merge two hashes? (http://www.velocityreviews.com/forums/t914827-faq-4-56-how-do-i-merge-two-hashes.html)

PerlFAQ Server 03-09-2011 05:00 PM

FAQ 4.56 How do I merge two hashes?
 
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.

C.DeRykus 03-09-2011 08:33 PM

Re: FAQ 4.56 How do I merge two hashes?
 
On Mar 9, 9:00*am, PerlFAQ Server <br...@theperlreview.com> 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 03-09-2011 08:39 PM

Re: FAQ 4.56 How do I merge two hashes?
 
On Mar 9, 12:33*pm, "C.DeRykus" <dery...@gmail.com> wrote:
> On Mar 9, 9:00*am, PerlFAQ Server <br...@theperlreview.com> 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



All times are GMT. The time now is 09:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.