#!/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....

In reply to Re: Checking contents of array against contents of a hash by Xiong
in thread Checking contents of array against contents of a hash by walkingthecow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.