Q&A > Data Structures
Description:
Ahh how to kkep this simple I need to generate 2+ hashes which I can then find intersection/difference between a master hash of set values. How can I carry out set theory with perl using hashes, or even arrays? For LARGE ammounts of key/value pairs?Answer: set theory w/hashes? arrays? done quickly? contributed by Fastolfe The Perl Cookbook has some excellent things to say on this topic, specifically in and about recipes 4.6 and 4.7. Hashes should be treated as an array of its keys when doing comparisons like this. In addition, see the Set::Scalar module, which is specifically designed to do manipulations and tests with sets. In a pinch, though, some code like this might help you (taken directly from the cookbook): If you need the difference: Set::Scalar makes this much more straightforward:

Answer: set theory w/hashes? arrays? done quickly? contributed by fundflow There has to be a better way using array slices...... got it
I don't really know how to remove elements from the hash. In the above example, the hash keys are the elements of the set. If you let the set be the elements in the hash that also have a nonzero value, then the same technique can be used for intersection:
Also, to answer the original poster: if "REALLY large" is really large, and doesnt fit in the main memory then you might want to store the sets in sorted files and merge them smartly to compute the union/intersection. 
Answer: set theory w/hashes? arrays? done quickly? contributed by clemburg You want to read Mastering Algorithms with Perl. Chapter 6 is about Sets. It also discusses the following Set Modules from CPAN:

Please (register and) log in if you wish to add an answer
 a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
For:  Use:  
&  &  
<  <  
>  >  
[  [  
]  ] 