Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Checking contents of array against contents of a hash

by Xiong (Hermit)
on Oct 06, 2010 at 20:29 UTC ( #863864=note: print w/ replies, xml ) Need Help??


in reply to Checking contents of array against contents of a hash

#!/run/bin/perl # for-walkingthecow-balls.pl # = Copyright 2010 Xiong Changnian <xiong@cpan.org> = # = Free Software = Artistic License 2.0 = NO WARRANTY = use 5.010; use strict; use warnings; use List::Compare; # Compare elements of two or more list +s #~ use Devel::Comments '###', '####'; #--------------------------------------------------------------------- +-------# # Declare hypothetical data set. # These are the things that may be distributed to containers, non-uniq +uely. my @balls = qw( red green blue yellow ); # These are the containers. Note the spares for further fooling around +. my %boxes = ( #~ round => [ qw( red green blue yellow ) ], #~ cube => [ qw( red green blue yellow ) ], #~ flat => [ qw( red green blue yellow ) ], round => [ qw( red ) ], cube => [ qw( green blue ) ], flat => [ qw( red green blue yellow ) ], ); # This is the goal state. my $want = 1; # How many boxes contain all balls? #--------------------------------------------------------------------- +-------# # Invoke code under test. say qq{Want: $want, Got: }, do_compare (\@balls, values %boxes ); exit(0); #--------------------------------------------------------------------- +-------# # Uses List::Compare. Calling syntax is highly flexible and orthogonal +. sub do_compare { my $unbox_ref = shift; my @boxes_refs = @_; my $got ; # accumulate "finds" here # Construct a work-object. my $lc = List::Compare->new( '-u', $unbox_ref, @boxes_refs ); # u +nsorted # Pretty-print for debug. $lc->print_subset_chart; # Find out if any (other) box contains all the elements in the unb +ox. my $ixL = 0; # the unbox; index of first ref to + new() for my $ixR ( 1..scalar @boxes_refs ) { my $bool = $lc->is_LsubsetR( $ixL, $ixR ); # true if all +L in R ### $bool $got += $bool; }; return $got; }; __END__ Subset Relationships Right: 0 1 2 3 Left: 0: 1 0 0 1 1: 1 1 0 1 2: 1 0 1 1 3: 1 0 0 1 Want: 1, Got: 1
Feste: Misprison in the highest degree. Lady, cucullus non facit monachum. That's as much to say as, I wear not motley in my brain....


Comment on Re: Checking contents of array against contents of a hash
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863864]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2015-07-06 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls