lmwtzw6u5l0 发表于 2017-5-19 10:39:56

perl集合运算之交集,并集,差集

  perl中,实现两个集合的运算很简单,只需几行代码即可

@a=('a'..'c',1..3);
@b=('A'..'C',1..3);
@union=();#并集
@diff=(); #差集
@isect=();#交集
foreach $e(@a,@b){
$union{$e}++&&$isect{$e}++;
}
@union=keys %union;
@isect=keys %isect;
@diff=grep {$union{$_}==1;} @union;
print (join ',',@union);
print "\n";
print (join ',',@isect);
print "\n";
print (join ',', @diff);
  输出:

A,a,3,B,2,c,1,C,b
1,3,2
A,a,B,c,C,b
页: [1]
查看完整版本: perl集合运算之交集,并集,差集