我有以下代码。如果没有附加的CPAN模块,isSubset
可以写得更简单吗?
my @possibleNames = ("adam", "chelsea");
my @actualNames = ("adam", "chucky", "chelsea");
sub isSubset {
my ($littleSet, $bigSet) = @_;
foreach (@{$littleSet}) {
return 0 unless ($_ ~~ @{$bigSet});
}
return 1;
}
printf("%s\n", (isSubset(\@possibleNames, \@actualNames) ? "yes" : "no"));
答案 0 :(得分:4)
一种相当有效的方法是:
sub isSubset {
my ($littleSet, $bigSet) = @_;
my %hash;
undef @hash{@$littleSet}; # add a hash key for each element of @$littleSet
delete @hash{@$bigSet}; # remove all keys for elements of @$bigSet
return !%hash; # return false if any keys are left in the hash
}
答案 1 :(得分:2)
my @possibleNames = ("adam", "chelsea");
my @actualNames = ("adam", "chucky", "chelsea");
my $is_subset = 0==grep !defined, map { @$_{@actualNames}=(1)x@actualNames; delete @$_{@possibleNames} } {};
但严重的是,使用Array :: Utils :: array_minus。