2013-07-29 47 views

回答

1
  1. 找到所有常见的元素,例如通过用散列进行重复数据删除。
  2. 查找所有不常见的元素。

给定两个数组@x@y,这将意味着:

use List::MoreUtils 'uniq'; 

# find all common elements 
my %common; 
$common{$_}++ for uniq(@x), uniq(@y); # count all elements 
$common{$_} == 2 or delete $common{$_} for keys %common; 

# remove entries from @x, @y that are common: 
@x = grep { not $common{$_} } @x; 
@y = grep { not $common{$_} } @y; 

# Put the common strings in an array: 
my @common = keys %common; 

现在,所有剩下的就是做了一下解引用和这样的,但应该是相当琐碎。

+0

@common的输出仅仅是一个@x和@y的CONCAT,不管他们是多么的相似或相异的真的很不错。 –

+0

@ PatrickRobertSheaO'Connor对不起,现在已经修复:我们必须计算每个元素有多少个数组,然后删除那些数量低于数组数的元素。 – amon

0

不需要其他模块。 Perl的哈希值是寻找uniq的或共同的价值观

my %both; 
# count the number of times any element was seen in 4 and 6 
$both{$_}++ for (@{$VAR1->{4}}, @{$VAR1->{6}}); 
for (keys %both) { 
    # if the count is one the element isn't in both 4 and 6 
    delete $both{$_} if($both{$_} == 1); 
} 
$VAR1->{Both} = [keys %both];