benedetto has asked for the wisdom of the Perl Monks concerning the following question:

I need to compare two arrays to make sure they do not share any of the same elements. For instance, if the two arrays are

@mammals = (dog, cat, mouse, horse, planet);

@sky = (star, planet, galaxy, nova, comet);

I want a program that would tell me, "planet is in both arrays." Or if no elements are shared, it would print "Both arrays are unique."

The actual arrays I have contain just numbers. Thousands of numbers, so it is too much to check them visually.

I have no clue how to do this.

Update

Should have written:

use strict; use warnings; @mammals = qw(dog cat mouse horse planet); @sky = qw(star planet galaxy nova comet);

Sorry. This is my first post.

Replies are listed 'Best First'.
Re: Compare two arrays for uniqueness
by Kenosis (Priest) on Dec 16, 2013 at 04:51 UTC

    The module List::Compare lives for this (and a few other things):

    use strict; use warnings; use List::Compare; my @mammals = qw/dog cat mouse horse planet/; my @sky = qw/star planet galaxy nova comet/; my $lc = List::Compare->new( \@mammals, \@sky ); my @intersection = $lc->get_intersection; print "@intersection", "\n"; # prints "planet" print scalar @intersection, "\n"; # prints 1 my @arr1 = 0 .. 5; my @arr2 = 6 .. 10; $lc = List::Compare->new( \@arr1, \@arr2 ); @intersection = $lc->get_intersection; print "@intersection", "\n"; # prints "" print scalar @intersection, "\n"; # prints 0
Re: Compare two arrays for uniqueness
by Cristoforo (Curate) on Dec 16, 2013 at 04:49 UTC
    At your command prompt, enter:

    perldoc -q "How do I compute the difference of two arrays"

Re: Compare two arrays for uniqueness
by AnomalousMonk (Archbishop) on Dec 16, 2013 at 05:05 UTC

    Actually, had this been meaningful code, you should have written (note use of my; see also our for global variables):

    use strict; use warnings; my @mammals = qw(dog cat mouse horse planet); my @sky = qw(star planet galaxy nova comet);

    Please see Use strict warnings and diagnostics or die.

Re: Compare two arrays for uniqueness
by Laurent_R (Canon) on Dec 16, 2013 at 07:13 UTC
    List::Compare is the way to go; however, assuming you wanted to do it manually yourself, there are two possibilities: to sort the arrays and then compare the elements one by one, or, better in most cases, to store one array in a hash and then check for elements of the other array that are in the hash;
Re: Compare two arrays for uniqueness
by Lennotoecom (Pilgrim) on Dec 16, 2013 at 08:38 UTC
    as example
    it is bad because:
    1. it tells nothing if there are no common elements, it's just quietly goes down)
    2. presumably arrays don't contain repeating elements themselves, but it should tell;
    though it will, if you change the foreach cycle to something like print "$k\n" if $h{$k} >= 2;
    @mammals = qw/dog cat mouse horse planet/; @sky = qw/star planet galaxy nova comet/; %h = map {$_ => $h{$_}++} @mammals, @sky; foreach $k (sort keys %h){ print "in both: $k\n" if $h{$k}; }
Re: Compare two arrays for uniqueness
by benedetto (Initiate) on Dec 16, 2013 at 05:18 UTC

    Great! I can do those.

    Grazie