There's more than one way to do things PerlMonks

### set theory w/hashes? arrays? done quickly?

 on Oct 03, 2000 at 20:25 UTC Need Help??
Contributed by Anonymous Monk on Oct 03, 2000 at 20:25 UTC
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): ```foreach \$e (@a, @b) { \$union{\$e}++ && \$isect{\$e}++ } @union = keys %union; @isect = keys %isect; [download]``` If you need the difference: ```@diff = (); foreach \$e (keys %union) { push(@diff, \$e) unless \$isect{\$e}; } [download]``` Set::Scalar makes this much more straightforward: ```\$s = new Set::Scalar (keys %hash1); \$t = new Set::Scalar (keys %hash2); @isect = \$s->intersection(\$t)->members; [download]``` Answer: set theory w/hashes? arrays? done quickly?contributed by fundflow There has to be a better way using array slices...... got it Here is an example for union: ```%s1=('a'=>1, 'b'=>1, 'c'=>1); %s2=('a'=>1, 'c'=>1, 'd'=>1); print "Set 1 has: ", join(",",keys %s1), " Set 2 has: ", join(",", k +eys %s2),"\n"; %j=%s1; @j{keys %s2}=1; print "Union is ", join(",", keys %j), "\n"; [download]``` 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: ```%inter=%s1; @inter{%s2}=0; [download]``` 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: Set::Scalar Set::Object Set::IntSpan Bit::Vector Set::IntRange

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
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
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
 [Corion]: It's close to my Perl script where you paste in the SQL and get the result as XLSX file, but I guess the Excel sheet is more user-friendly :) [choroba]: What drivers does it support? ;-) [erix]: sounds cool -- but now you have to give SQL lessons?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-07-18 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (392 votes). Check out past polls.

Notices?