Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to join two hashes?

2 views
Skip to first unread message

Federico Abascal

unread,
Apr 20, 1999, 3:00:00 AM4/20/99
to
Hello,
does anybody knows how to join two hashes, if possible? (The two hashes
have some keys similar)
Thanks,
Federico


Larry Rosler

unread,
Apr 20, 1999, 3:00:00 AM4/20/99
to
[Posted and a courtesy copy sent.]

In article <371C5AB4...@gredos.cnb.uam.es> on Tue, 20 Apr 1999
12:45:09 +0200, Federico Abascal <faba...@gredos.cnb.uam.es> says...


> does anybody knows how to join two hashes, if possible? (The two hashes
> have some keys similar)

The hash-slice approach is efficient. The values of the shared keys
from the second hash overwrite those of the original hash.

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

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/
l...@hpl.hp.com

Joe McMahon

unread,
Apr 20, 1999, 3:00:00 AM4/20/99
to
In article <371C5AB4...@gredos.cnb.uam.es>, Federico Abascal
<faba...@gredos.cnb.uam.es> wrote:

>Hello,


>does anybody knows how to join two hashes, if possible? (The two hashes
>have some keys similar)

>Thanks,
>Federico

If you mean merge them - i.e., you have %a and %b, and you want to put them
in %c without worrying about whether there are duplicate keys, try

%c = (%a, %b);

Otherwise, if you want to retain the duplicates, try Tie::AppendHash.

--- Joe M.

Tom Christiansen

unread,
Apr 20, 1999, 3:00:00 AM4/20/99
to
[courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,
l...@hpl.hp.com (Larry Rosler) writes:
:The hash-slice approach is efficient. The values of the shared keys

:from the second hash overwrite those of the original hash.
:
: @hash1{keys %hash2} = values %hash2;

Not for huge hashes it isn't -- imagine a hash tied to a ten-million
element DBM file, for example. But for most cases, this is fine.

--tom
--
The steady state of disks is full.
--Ken Thompson

0 new messages