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

Hello. Could someone please tell me if I've found (a) a bug in Array::Compare, (b) some 5.10 weirdness, or (c) if I am just making a really obvious mistake?

My code, taken almost entirely from the synopsis:

#!/usr/bin/perl -w use strict; use warnings; use Array::Compare; my @array1 = [1, 2]; my @array2 = [2, 1]; my $comp = Array::Compare->new; if ($comp->perm (\@array1, \@array2)) { print "Arrays are perms\n"; } else { print "Nope. Arrays are completely different\n"; }

The output, which is the same under Cygwin and Strawberry, both 5.10:

Nope. Arrays are completely different

They should be permutations, yes?

Strawberry Perl, on Win XP Pro SP3:

This is perl, v5.10.0 built for MSWin32-x86-multi-thread Copyright 1987-2007, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

Thanks in advance!

planetscape

Replies are listed 'Best First'.
Re: Bug in Array::Compare?
by Corion (Patriarch) on Apr 07, 2009 at 07:38 UTC

    Did you want to initialize the arrays with array references? I'm not sure whether Array::Compare is supposed to do deep comparisons, and then it stands to debate whether two different array references make the array contents different or not.

Re: Bug in Array::Compare?
by citromatik (Curate) on Apr 07, 2009 at 07:57 UTC

    You are initializing your arrays with array references, and the documentation of the module states that it works by using join to turn both arrays into strings and comparing the strings using eq, so, assuming that it makes only one level of dereferencing, this means that internally, it makes the comparison between "[1,2]" and "[2,1]" which are not the same string.

    In fact, the same problem arises when using the same array in the references:

    print $comp->compare ([[1,2]],[[1,2]]) ? "Yes\n" : "No\n"; print $comp->compare ([1,2],[1,2]) ? "Yes\n" : "No\n";

    prints:

    No Yes

    Hope this helps,

    citromatik