Get the intersection of two lists of strings in Perl

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

    Here's one map/grep approach:

    my @a = qw(Perl PHP Ruby Python C JavaScript);
    my @b = qw(erlang java perl python c snobol lisp);
    my @intersection =
        grep { defined }
            @{ { map { lc ,=> $_ } @a } }
               { map { lc } @b };
    
    # @intersection = qw(Perl Python C)
    

提交回复
热议问题