Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Answer: Union of Two Hashes

by maverick (Curate)
on Apr 09, 2002 at 14:35 UTC ( #157735=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > Union of Two Hashes - Answer contributed by maverick

The question is a bit confusing...you ask for a union, but you code snippet looks like you're looking for the intersection. Union first. For small hashes this is really quick:
%union = (%first, %second);
For large:
while (($k,$v) = each %first) { $union{$k} = $v; } while (($k,$v) = each %second) { $union{$k} = $v; }
(makes sense once you consider the memory usage for the first snippet.

For intersection, all you really need to do is change $matchWords to $matchWords{$k}++ the iterate of then matchWords looking for values == 2

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2017-11-22 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (327 votes). Check out past polls.

    Notices?