We are going to start off by looking at somethng we have all done. The given example comes fron a great article on adding search functionality to Perl Applications
my %stopwords; @stopwords{(qw(a i at be do to or is not no the that they then these them who where why can find on an of and it by))} = 1 x 27;
What's he doing here? He is building a hash so that later on he can check for stopwords like so:
my @salient_word = grep { not $stopwords{$_} } @word ;

Now, what is wrong with what he did? Well, nothing, but there is a module on CPAN, Set::Scalar that makes this tasks more DWIM instead of DWIS.

Here is how the same coding of stop words would be done using it:

use Set::Scalar; $stopwords = Set::Scalar->new; $stopwords->insert( qw(a i at be do to or is not no the that they then these them who where why can find on an of and it by)); @word = split /\s+/, "Oh say can you see by the dawn's early light";
And then we check it like this:
my @salient_word = grep { not $stopwords->has($_) } @word ;
or like this
$word = Set::Scalar->new(@word); my $salient_word = $word - $stopwords; # or this: my $salient_word = $word->difference($stopwords);

Other Goodies

  • $s->invert(@members); : insert if it isnt in set, delete if it is. Or, codewise:
    $members = Set::Scalar->new(@members); my $not_in = $s - $members; my $in = $s->intersect($members); $s->delete($in->members); $s->insert($not_in->members);
  • union, intersection, difference : you know what these do.
  • symmetric_difference : the symmetric difference of sets $a and $b is performed like this:
    $N = ($a - $b); $N->insert($b - $a);
    In other words, it is ($a - $b) UNION ($b - $a)
  • unique : I don't understand this one.
  • complement : I don't understand this one.

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.


In reply to Set::Scalar saves you from hash acrobatics by princepawn

Title:
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, details, 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, summary, 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.