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

Im a complete newbie at Perl and trying to learn by watching others code and reading docs, so bear with me.
I have two arrays, and I know that the first array contains some values that the second array does not. I need these values.
So far, I've got this:
my $comp = Array::Compare->new(DefFull => 1); my @differences=$comp->compare(\@array1,\@array2);

Now what? How do I get the values out?
I tried this:
foreach $value (@differences) { print $value; }
but it just gave me a bunch of numbers.

Im sure Im doing something wrong and use the Array::Compare incorrectly, but the Array::Compare documentation mentions nothing about how to get the actual values out, and I have found no examples to look at.
Any help, snippets of code or pointers to documentations appreciated.

Replies are listed 'Best First'.
Re: comparing arrays
by chester (Hermit) on Aug 29, 2005 at 18:52 UTC
    From the Array::Compare documentation

    In scalar context returns the number of columns that differ (zero if the arrays are the same). In list context returns an list containing the indexes of the columns that differ (an empty list if the arrays are the same).

    Array::Compare doesn't look like a fun module. There are a lot of ways to do this though. If you know one array is a superset of another, and if you don't care about possible duplicates in the arrays, you could use a hash like this:

    use warnings; use strict; my @array1 = (1,9,2,3,4,8,5); my @array2 = (1, 2,3,4, 5); my %hash; @hash{@array2} = (); foreach my $item (@array1) { print "$item\n" if ! exists $hash{$item}; }
Re: comparing arrays
by xdg (Monsignor) on Aug 29, 2005 at 18:55 UTC

    A few comments:

    1. If you're just starting out, you might want to try this "by hand" -- it's not that hard to do and you'll learn more about Perl than by relying on an external module.

    2. Are you trying to compare if the values of two arrays of equal length are the same? Or if one array contains any values not in the other? (I.e. does exact position matter?) I'm not sure if Array::Compare is what you want or if you want List::Compare. Array::Compare looks like it's for comparing two arrays of equal length only.

    3. A quick skim of Array::Compare reveals this:

    full_compare \@ARR1, \@ARR2 Do a full comparison between two arrays. Checks each individual column. In scalar context returns the number of + columns that differ (zero if the arrays are the same). In list conte +xt returns an list containing the indexes of the columns that differ +(an empty list if the arrays are the same).

    I'd guess your @difference array has the indices where there are differences, so you can "get the values out" like this:

    foreach my $i (@differences) { print "$array1[$i] is not equal to $array2[$i]\n"; }

    There are other ways to use an array of indices to retreive values from an array, such as slices (see perldata) but the example above should get you started.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: comparing arrays
by Codon (Friar) on Aug 29, 2005 at 18:44 UTC
      Thanks a lot for all the replies.

      Ive found the answer by following one of the links.
      This does exactly what I wanted:
      my %array1=map{$_ =>1} @array1; my %array2=map{$_=>1} @array2; my @differences=grep(!defined $array1{$_}, @array2); foreach $difference (@differences) { $differences++; print "$difference \n"; }
      I cant say that I exactly understand what that code does, but I will figure it out somehow. I understand the grep and !defined part, but not the %array1=map... and $array1{$_}. But I'll read up on it, I refuse to use code that I do not understand, even if it works.
      And to all those other that responded. Thanks for the help, I realize now that I did not understand how to use the Array::Compare module correctly.
Re: comparing arrays
by davidrw (Prior) on Aug 29, 2005 at 18:55 UTC
    Your output looks ok.. can you clarify what "bunch of numbers" means? I think it might have been working for you, but it A) might not have been in the order you expected and B) run together, which you can easily fix with a newline:
    foreach $value (@differences) { print $value . "\n"; }