How can I compare arrays in Perl?

后端 未结 12 2090
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 18:54

I have two arrays, @a and @b. I want to do a compare among the elements of the two arrays.

my @a = qw\"abc def efg ghy klm ghn\";
m         


        
12条回答
  •  不思量自难忘°
    2020-12-06 19:10

    Check to create an intersect function, which will return a list of items that are present in both lists. Then your return value is dependent on the number of items in the intersected list.

    You can easily find on the web the best implementation of intersect for Perl. I remember looking for it a few years ago.

    Here's what I found :

    
    my @array1 = (1, 2, 3);
    my @array2 = (2, 3, 4);
    my %original = ();
    my @isect = ();
    
    map { $original{$_} = 1 } @array1;
    @isect = grep { $original{$_} } @array2;
    
    

提交回复
热议问题