Get the intersection of two lists of strings in Perl

前端 未结 4 461
粉色の甜心
粉色の甜心 2021-01-02 07:50

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

4条回答
  •  囚心锁ツ
    2021-01-02 08:10

    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";
    

    This produces the expected output of

    c,d
    

    Edit: 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).

提交回复
热议问题