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

Hi All, How to compare each element of Array A with Array B then SKIP THE MATCHING elements and PRINT the ones not in Array B? Both arrays are number lists. Thanks
  • Comment on Comparing each element of Array A to Array B

Replies are listed 'Best First'.
Re: Comparing each element of Array A to Array B
by thezip (Vicar) on Aug 09, 2007 at 16:00 UTC

    Here's the canonical WTDI:

    #!/perl/bin/perl use strict; use warnings; my @arrA = qw/ 1 3 5 7 9 11 13 /; my @arrB = qw/ 1 4 9 16 25 /; my %hashB = map { $_ => 1 } @arrB; # Hashify the B array for later com +parison print qq(These numbers are in array A:\n); print join(" ", @arrA); print "\n\n"; print qq(These numbers are in array B:\n); print join(" ", @arrB); print "\n\n"; print qq(These numbers are in array A, but not array B:\n); for my $curA (@arrA) { unless (exists($hashB{$curA})) { print " $curA"; } } print "\n\n";

    Output:

    These numbers are in array A: 1 3 5 7 9 11 13 These numbers are in array B: 1 4 9 16 25 These numbers are in array A, but not array B: 3 5 7 11 13

    Where do you want *them* to go today?
Re: Comparing each element of Array A to Array B
by EvanK (Chaplain) on Aug 09, 2007 at 15:38 UTC
    You might find Array::Diff helpful.

    __________
    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

Re: Comparing each element of Array A to Array B
by citromatik (Curate) on Aug 09, 2007 at 15:54 UTC

    You can use hashes:

    my @arr1 = qw/1 3 4 5 6/; my @arr2 = qw/1 2 4 5 7 10/; my %hash2; @hash2{@arr2} = ("1") x @arr2; for (sort {$a <=> $b} @arr1){ print "$_\n" if (! defined $hash2{$_}); }

    Prints:

    3 6

    citromatik

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Comparing each element of Array A to Array B
by FunkyMonk (Bishop) on Aug 09, 2007 at 15:40 UTC
    See PerlFAQ4, or type perldoc -q difference at a command prompt.

Re: Comparing each element of Array A to Array B
by eric256 (Parson) on Aug 09, 2007 at 16:27 UTC

    Lots of ways to do it, loops, grep, hashes, etc. If you want all the elements in A that arn't B then you can use grep. Grep will return a list of elements matching a criteria. So you mix two grep statments together, the inner most will check if a given element $a is in array @b, the outer grep will go over @a, setting each element to $a and then running the inner test. If the inner test is true then we include the element. I reversed the inner test with a ! to make it show the ones NOT in @B, removing the ! would make it print only those matching elements.

    I then did the same with for and grep, and then finaly with two for loops. Hopefully this will help you see how the original solution works and how much easier it is ;)

    #!/usr/bin/perl use strict; use warnings; my @a = (1,2,3,4,5); my @b = (1,3,4); print "Using Grep:"; my @c = grep { $a = $_; !grep { $_ eq $a } @b } @a; print join(",", @c), "\n"; print "Using For and Grep:\n"; for $a (@a) { print $a,"\n" if !grep { $a eq $_ } @b; } print "Using only For:\n"; for $a (@a) { my $found = 0; for $b (@b) { $found = 1 if $a eq $b; } print $a,"\n" if !$found; }

    ___________
    Eric Hodges
      The difference between your solutions and those previously posted is speed. Yours is O(N*N), whereas those previously posted are a much faster O(N).

        True, but the example data was small and some times its more important to do it in a way that makes sense than the fastest way. I know when i started hashes where confusing concepts since they barely exist in other language, true they are now one of my favorites, but if you are new and someone solves an array problem with a hash solution it can be confusing.


        ___________
        Eric Hodges
      Thanks Eric! PerlMonks is really great! Iam starting to learn Perl. Any suggestions...

        Yep, PerlMonks :-D. Take a look through the Tutorials section, and if you have any questions, you know where to ask.

        It's also worth looking at the Seekers of Perl Wisdom section. Many of the questions posted there will be obscure or way beyond your present knowledge, but many will introduce bits of Perl and ways of using it that don't leap off the pages of a text book. Be prepared to do some digging in the Perl documentation (see perlintro for a start then perltoc).


        DWIM is Perl's answer to Gödel