How can I compare arrays in Perl?

后端 未结 12 2089
隐瞒了意图╮
隐瞒了意图╮ 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:25

    This is one way:

    use warnings;
    use strict;
    my @a = split /,/, "abc,def,efg,ghy,klm,ghn";
    my @b = split /,/, "def,ghy,jgk,lom,com,klm";
    my $flag = 0;
    my %a;
    @a{@a} = (1) x @a;
    for (@b) {
        if ($a{$_}) {
            $flag = 1;
            last;
        }
    }
    print "$flag\n";
    

提交回复
热议问题