Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Logical operations on arrays

by ronmrdechai (Novice)
on Oct 04, 2012 at 12:16 UTC ( #997226=perlquestion: print w/ replies, xml ) Need Help??
ronmrdechai has asked for the wisdom of the Perl Monks concerning the following question:

Can logical operations be preformed on arrays in perl? (through built in functionality or through a module of some kind)

e.g. (a, b, c) AND (b, c, d) = (b, c).

Google has failed me on this one.

Comment on Logical operations on arrays
Re: Logical operations on arrays
by MidLifeXis (Prior) on Oct 04, 2012 at 12:21 UTC

    If the order of the arrays makes no difference, it sounds like you are looking for an intersection operator of some sort. Array::Utils might have what you are looking for. I found this by searching metacpan for "union intersect".

    Otherwise, a method to find them is pretty straightforward:

    • configure a hash for the first array (*)
      @hash1{@array1} = (1)x@array1
      This configures a hash %hash1 to have a true value for all values found in @array1. (* - This is probably clearer as $hash1{$_} = 1 for @array1)
    • loop through the second array picking out those elements that show up in the first array
      push( @intersection, grep { $hash1{$_} } @array2 );
    There are a couple of assumptions built into the above (@arrayN does not contain an undef value, and the second array only has unique values).


    1. Added example code
    2. Added search example
    3. D'oh - quite right Jenda. I have got to stop writing code before my first cup of coffee. :-/


      There's no reason to work hard to set the values in the hash to ones, leave them undefined and use exists() within the grep{}. Also there is no reason to push the elements into the @intersection, just assign them.

      my %hash1; @hash1{@array1} = (); my @intersection = grep {exists $hash1{$_} } @array2;

      Enoch was right!
      Enjoy the last years of Rome.

Re: Logical operations on arrays
by daxim (Chaplain) on Oct 04, 2012 at 13:07 UTC
    These are set operations, see Set::Scalar or similar module.
Re: Logical operations on arrays
by Kenosis (Priest) on Oct 04, 2012 at 15:59 UTC

    One option is to use List::Compare:

    use strict; use warnings; use List::Compare; my @a = qw(a b c); my @b = qw(b c d); print "\@a = @a\n\@b = @b\n\n"; print '@a AND @b = ', arrayANDCompare( \@a, \@b ), "\n\n"; print '@a OR @b = ', arrayORCompare( \@a, \@b ); sub arrayANDCompare { my ( $a, $b ) = @_; return List::Compare->new( $a, $b )->get_intersection; } sub arrayORCompare { my ( $a, $b ) = @_; return List::Compare->new( $a, $b )->get_union; }


    @a = a b c @b = b c d @a AND @b = bc @a OR @b = abcd

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997226]
Approved by marto
Front-paged by Old_Gray_Bear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-09-24 04:47 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (245 votes), past polls