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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is a great idea. In fact, its such a great idea that its been done many times before. :) You've definitely hit on a useful technique.

This is pretty similar to recipe 4.8 in the Perl Cookbook 1st edition.

foreach $e (@a, @b) { $count{$e}++ } foreach $e (keys %count) { push(@union, $e); if ($count{$e} == 2) { push @isect, $e; } else { push @diff, $e; } }

Here's my implementation with each operation calculated separatly.

use strict; use warnings; my @one = qw( 1 2 3 5 6 8 10 ); my @two = qw( 2 4 6 8 10 ); print "One = @one\n"; print "Two = @two\n"; print "\n"; my %union; @union{@one, @two} = (); my @union = sort keys %union; print "Union = @union\n"; my (%one, %two); @one{@one} = (); @two{@two} = (); my %intersection = map { exists $one{$_} && exists $two{$_} ? ($_ => undef) : () } @one, @two; my @intersection = sort keys %intersection; print "Intersection = @intersection\n"; my %one_minus_two = map { exists $two{$_} ? () : ($_ => undef) } @one; my @one_minus_two = sort keys %one_minus_two; print "One - Two = @one_minus_two\n"; my %symdiff = map { 1 == ( exists($one{$_}) + exists($two{$_}) ) ? ($_ => undef) : () } @one, @two; my @symdiff = sort keys %symdiff; print "Symdiff = @symdiff\n";

If I was doing a whole lot of set manipulation I'd look for a library that provides a complete suite of set theoretic operations--like Set::Object or Set::Scalar. For simple uniqueness, I'd probably stick with List::MoreUtils--some of its other functions like 'any' and 'all' offer such great readability improvements to my code that I have come to rely on it.

TGI says moo

In reply to Re: Idiom: hashes as sets by TGI
in thread Idiom: hashes as sets by vrk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
  • Please read these before you post! —
  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2018-02-21 23:30 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (288 votes). Check out past polls.