Re: SET Operations in Perl

From: Brian McCauley (nobull_at_mail.com)
Date: 02/16/05


Date: Wed, 16 Feb 2005 17:55:18 +0000


Gregory Toomey wrote:

> George wrote:
>
>
>>Hi,
>>how can we do SET Operations in perl using two hashes or arrays
>>in case it is too easy , please redirect me to some link
>>thanks
>
>
> http://www.gregorytoomey.com/index.php?option=content&task=view&id=9&Itemid=28

Those are good if you have sets in arrays.

But if you have to store sets in Perl then arrays are not the natural type.

The natural type in Perl is the valueless hash (i.e. one with only keys).

Some what one usually thinks of as the primative set operations are not
primative in this implementation but the primatives you do have are
sufficient.

The primatives you get are:

Add a list of elements to set:

   @set{'element1','emement2'} = ();

Remove a list of elements from set:

   delete @set{'element1','emement2'};

Enumerate elements of set:

   my @elements = keys %set;

Test if one element is in a set:

   exists $set{$element};

Reduce a list to those elements in a set (also test if ant of a list is
in the set in a scalar context):

   grep { exists $set{$_} } LIST;

(Note for large data there is an unpleasant overhead for using grep in a
scalar context so for large data don't - roll your own loop).

Union is not as often wanted as you may think - most of the time people
think they want union they can really make do with "add a list elements
to a set".

   my %union = ( %set1, %set2 );

Intersection is non-primative but is not as often wanted as you may
think - most of the time people think they want intersection they can
really make do with "reduce a list to those elements in a set".

   my %intersection;
   @intersection{ grep { exists $set1{$_} keys %set2 }} = ();



Relevant Pages

  • Re: SET Operations in Perl
    ... >> how can we do SET Operations in perl using two hashes or arrays ...
    (comp.lang.perl.misc)
  • Re: Idiom for array index that Im foreaching over?
    ... >>you'll find that you rarely want to loop over an index. ... With hashes, indexed access, and hence loops ... > simplicity and elegance that I expect out of good modern Perl ... If, for some reason, the data is given in individual arrays, there are ...
    (comp.lang.perl.misc)
  • Re: Fw: question
    ... As a matter of fact Perl has: ... With the above functions and arrays and hashes there are very few data ... Also, PHP is a very different language then Perl, remember that. ... What happens if I add or remove keys from a hash while iterating over it? ...
    (perl.beginners)
  • Re: delimited data into nested array
    ... > Perl as a two dimensional nested array. ... Do you have a good reason for reading the file into an array ... If you'd still like to use the arrays, read up on perllol and use this: ... This whole loop was condensed above by using join, $\, and that handy ...
    (comp.lang.perl.misc)
  • Re: math operations
    ... I just want to know if I can do the following things with perl: ... However, from the series of articles you've been posting lately, it ... helping people to help you, you don't have clear ideas about ... "arrays with their elements being bits". ...
    (comp.lang.perl.misc)