Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

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

Dear monks,

recently I have found myself wanting to use sets, i.e. collections of unique values. I could use a CPAN package for it, but more often than not I simply write

my %set = (); my @new_items = ...; @set{@new_items} = (1) x @new_items;

Deleting items from the set is of course

delete @set{@other_items};

I don't know how common the idiom is. It may be inefficient with large sets and arrays, and the syntax is a bit peculiar for adding things to the set, but it saves conceptual effort and reduces running time compared to sorting and removing duplicate items later. You can also always reduce the set (hash) to an array with [keys %set].

Are there better ways to do it? Here, better can be any one of conceptually or syntactically clearer, less memory wasted in storing the items, or less memory wasted in adding items to the set.

A further advantage of the above idiom is that you can compute set difference and symmetric difference easily:

# Difference. sub difference { my %diff = %{ $_[0] }; delete @diff{ keys %{ $_[1] } }; \%diff; } # Symmetric difference. sub symmetric_difference { my %symm = (); my @set1 = keys %{ difference($_[0], $_[1]) }; my @set2 = keys %{ difference($_[1], $_[0]) }; @symm{@set1, @set2} = (1) x (@set1 + @set2); \%symm; }

An obvious problem with the idiom is auto-vivication, but as long as you're careful and consistent, it won't bite you.

print "Just Another Perl Adept\n";

In reply to 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 wandering the Monastery: (5)
    As of 2017-12-18 21:47 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (499 votes). Check out past polls.