这个问题在这里已经有了答案:
Difference of Two Arrays Using Perl
(10 个回答)
8年前关闭。
我有以下代码。可以isSubset
如果没有附加的 CPAN 模块,编写起来会更简单吗?
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"));
最佳答案
一种相当有效的方法是:
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
}
关于Perl - 确定一个数组是否是另一个数组的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16947435/