Here's another option using smart matching:

use Modern::Perl; my $a = "AB:APPLE:BOY:ZOO:ZOOM"; my $b = "APPLE:ZOO"; my $c = "BOY:GIRL"; say isSubset( $b, $a ); #Returns 1 say !isSubset( $c, $a ); #Returns 1 sub isSubset { my @small = split ':', $_[0]; my @big = split ':', $_[1]; @small ~~ [ grep $_ ~~ @big, @small ]; }

Update: The subroutine below might be more efficient, since it's not building a list (the results are the same):

sub isSubset { my $count = my @small = split ':', $_[0]; my @big = split ':', $_[1]; do { $count-- when @big } for @small; !$count; }

Here's an interesting read: How fast is Perl's smart match operator for searching scalar in an array?.

UpdateUpdate: Ran a benchmark on three subroutines having different set/subset algorithms:

use Modern::Perl; use Benchmark qw(cmpthese); my $a = "AB:APPLE:BOY:ZOO:ZOOM"; my $b = "APPLE:ZOO"; my $c = "BOY:GIRL"; sub isSubset { my @small = split ':', $_[0]; my @big = split ':', $_[1]; @small ~~ [ grep $_ ~~ @big, @small ]; } sub isSubsetSmarter { my $count = my @small = split ':', $_[0]; my @big = split ':', $_[1]; do { $count-- when @big } for @small; !$count; } sub isSubsetHash { my $count = my @small = split ':', $_[0]; my %big; $big{$_} = 1 for split ':', $_[1]; do { $count-- if $big{$_} } for @small; !$count; } cmpthese( -5, { isSubset => sub { isSubset( $b, $a ) }, isSubsetSmarter => sub { isSubsetSmarter( $b, $a ) }, isSubsetHash => sub { say isSubsetHash( $b, $a ) }, } );

Results:

Rate isSubset isSubsetHash isSubsetSmarter isSubset 143452/s -- -15% -54% isSubsetHash 169263/s 18% -- -45% isSubsetSmarter 309705/s 116% 83% --

The original isSubset is the slowest, but isSubsetSmarter is surprisingly the fastest of the crew--at least with these small data sets.


In reply to Re: Computing Subsets by Kenosis
in thread Computing Subsets by grandpascorpion

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.