Get the intersection of two lists of strings in Perl

前端 未结 4 463
粉色の甜心
粉色の甜心 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:21

    Smallest amount of change required from the original solution. Just lower-case the strings.

    @a = qw( a b c d );
    @b = qw( C D E F );
    ...
    foreach $e (@a, @b) { 
        $union{lc $e}++ && $isect{lc $e}++ 
    }
    
    @union = keys %union;
    @isect = keys %isect;
    

提交回复
热议问题