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

I'm using List::Compare to compare the Arrays in a Hash of Arrays. I can specify each array in the hash spicifically but since the hash may contain anywhere from 1 to 5 arrays, I'd like to pass all the arrays, without specifiying them, into the List::Compare constructor all at once using just the hash itself.

I've read the documentation for List::Compare but I can't figure it out.

Instead of constructing it like this:
List::Compare->new( \@{$hash{0}}, \@{$hash{1}}, \@{$hash{2}} );

I'd like consruct it like this just using the hash:
List::Compare->new( \%hash );

Here's the full code:

#!/usr/bin/perl -w use strict; use List::Compare; my @primary = qw( 1 2 3 4 ); my @secondary = qw ( 3 4 5); my @another = qw ( 4 5 6 7 ); my %hash; $hash{0} = \@primary; $hash{1} = \@secondary; $hash{2) = \@another; my $lc = List::Compare->new( \@{$hash{0}}, \@{$hash{1}}, \@{$hash{2}} +); # Would like to pass and compare every array in the hash without speci +fying each array in the hash. # my $lc = List::Compare->new( %hash ); # But I can't figure out how to get it to work my @intersection = $lc->get_intersection; print @intersection;

Replies are listed 'Best First'.
Re: Comparing multiple arrays in Hash of Arrays using List::Compare
by BrowserUk (Patriarch) on Feb 13, 2006 at 22:31 UTC

    Try

    List::Compare->new( values %hash );

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      That works but I don't understand why. Can you explain?

      I would think that just passes the key names to the constructor. But obviously that's wrong.

      Edit: I see, you used values and not key names.

        Because the contructor is expecting a list of array references, and that is exactly what the values of your hash are.

        The downside of using values here is that you do not know what order the array references are passed to the constructor which could make interpreting the results difficult. If this is a problem, then you can define that order by using a hash slice like this:

        my $lc = List::Compare->new( @hash{ sort keys %hash } );

        which will pass the array references to the constructor in whatever order you sort the keys. As shown above, this would be alphanumeric ascending, but as your keys are numeric (why aren't you using an array instead of a hash?), you might prefer

        my $lc = List::Compare->new( @hash{ sort{ $a <=> $b} keys %hash } );

        depending upon how you intend to use the results.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.