Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

set theory w/hashes? arrays? done quickly?

by Anonymous Monk
on Oct 03, 2000 at 20:25 UTC ( [id://35134]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (data structures)

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?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: set theory w/hashes? arrays? done quickly?
by Fastolfe (Vicar) on Oct 03, 2000 at 21:51 UTC
    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;
    If you need the difference:
    @diff = (); foreach $e (keys %union) { push(@diff, $e) unless $isect{$e}; }
    Set::Scalar makes this much more straightforward:
    $s = new Set::Scalar (keys %hash1); $t = new Set::Scalar (keys %hash2); @isect = $s->intersection($t)->members;
Re: set theory w/hashes? arrays? done quickly?
by fundflow (Chaplain) on Oct 04, 2000 at 03:02 UTC
    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";
    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;


    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.

      I don't really know how to remove elements from the hash.

      That reminded me that you can subtract %h2 from %h1 via:

      delete @h1{ keys %h2 };

              - tye (but my friends call me "Tye")
Re: set theory w/hashes? arrays? done quickly?
by clemburg (Curate) on Oct 04, 2000 at 19:57 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://35134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found