Perl中获取两个字符串列表的交集 [英] Get the intersection of two lists of strings in Perl
本文介绍了Perl中获取两个字符串列表的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在第 4 章第 4.8 节(计算唯一列表的联合、交集或差异)中,Perl Cookbook 提供了获取两个整数列表交集的技术:
In Chapter 4, Section 4.8 (Computing Union, Intersection, or Difference of Unique Lists), the Perl Cookbook provides this technique for getting the intersection of two lists of integers:
@a = (1, 3, 5, 6, 7, 8);
@b = (2, 3, 5, 7, 9);
...
foreach $e (@a, @b) {
$union{$e}++ && $isect{$e}++
}
@union = keys %union;
@isect = keys %isect;
我希望对两个字符串列表执行此操作(不区分大小写).请问有什么有效的方法吗?
I want this to be done (case-insensitively) for two lists of strings. Any efficient method, please?
推荐答案
Array::Utils 正是您要找的.p>
Array::Utils is what you're looking for.
use Array::Utils qw(:all);
my @a = qw( a b c d );
my @b = qw( c d e f );
my @isect = intersect(@a, @b);
print join(",",@isect) . "\n";
这会产生预期的输出
c,d
我没有注意到您希望不区分大小写地完成此操作.在这种情况下,您可以将 @a
替换为 map{lc}@a
(同样也可以替换为 @b
).
I didn't notice that you wanted this done case-insensitively. In that case, you can replace @a
with map{lc}@a
(and likewise with @b
).
这篇关于Perl中获取两个字符串列表的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文