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,b1,3,2A,a,B,c,C,b
?