我如何才能找到了在一个阵列,但不是另一个元素在Perl? [英] How can I find elements that are in one array but not another in Perl?
本文介绍了我如何才能找到了在一个阵列,但不是另一个元素在Perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组,我想找到了在一个数组中的元素,但不是另一个:
例如:
@数组1 =(ABC,CDE,FGH,IJK,LMN)
@数组2 =(ABC,FGH,LMN)
我需要到结束:
@ ARRAY3 =(CDE,IJK)
解决方案
把第二个数组的元素融入到一个哈希,高效的检查,看看是否没有特定的元素在,然后过滤第一阵列只是这不是第二个数组中的元素:
我的%array2_elements;
@ array2_elements {@数组2} =();
我@ ARRAY3 = grep的!存在$ array2_elements {$ _},@数组1;
I have two arrays and I want to find elements that are in one array but not another:
ex:
@array1 = ("abc", "cde", "fgh", "ijk", "lmn")
@array2 = ("abc", "fgh", "lmn")
I need to end up with:
@array3 = ("cde", "ijk")
解决方案
Put the elements of the second array into a hash, for efficient checking to see whether or not a particular element was in it, then filter the first array for just those elements that were not in the second array:
my %array2_elements;
@array2_elements{ @array2 } = ();
my @array3 = grep ! exists $array2_elements{$_}, @array1;
这篇关于我如何才能找到了在一个阵列,但不是另一个元素在Perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文