首页 > 代码库 > Perl 数组 交集,补集,并集

Perl 数组 交集,补集,并集

my @a=("a","b","c","d","e");
my @b=("b","g","f","e");

my %a=map{$_=>1} @a;
my %b=map{$_=>1} @b;

# 交集
@A = grep( $a{$_}, @b ) ;

# 补集
@B1=grep(!defined $a{$_}, @b);
@B2=grep(!defined $b{$_}, @a);

# 并集 
%C=map {$_=>1} @a,@b;
@C=keys %C;