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

I'm comparing multiple lists with List::Compare repeatedly without creating a new List::Compare object for each new comparison. It appears that List::Compare is returning the result of the first comparision.

#!/usr/bin/perl -w use strict; use List::Compare; my @one = qw( 384 425 ); my @two = qw( 384 425 466 507 548); my @three = qw( 466 507 ); my @four = qw( 666 999 ); my $lc = List::Compare->new( \@one, \@two ); my @intersection = $lc->get_intersection; print "Out 1: @intersection\n"; $lc->new( \@three, \@two ); @intersection = $lc->get_intersection; print "Out 2: @intersection\n"; $lc->new( \@four, \@two ); @intersection = $lc->get_intersection; print "Out 3: @intersection\n";
Here's my output:
Out 1: 384 425 Out 2: 384 425 Out 3: 384 425
It should look like this:
Out 1: 384 425 Out 2: 466 507 Out 3:
Is this a bug? I didn't see it in the documentation for List::Compare, but do I have to flush the lists out of the List::Compare object first? Or do I have to create a new object for each new comparision.

Edit:
Perl version = 5.8.7
List::Compare version = 0.32

Replies are listed 'Best First'.
Re: Bug in List::Compare or User Error?
by McDarren (Abbot) on Feb 15, 2006 at 06:44 UTC
    I suspect that the following line is incorrect:
    $lc->new( \@three, \@two );
    Try changing that to:
    $lc = List::Compare->new( \@three, \@two );
    (and ditto for the subsequent similar lines)

    Cheers,
    Darren :)

      That worked! Thanks.