Perl:如何检查整数列表中是否有任何整数在数组中

Perl:如何检查整数列表中是否有任何整数在数组中

问题描述:

类似于我之前的问题,但是这次我想知道整数列表中是否有任何整数在数组中。

Similar to my earlier question, but this time I would like to know if any integers in a list of integers are in an array.

例如,如果我有:

@int_array = (7,101,80,22,42);

如何检查整数值80、77或99是否在数组中?有没有比对列表中的每个整数单独进行智能匹配更好的方法?我正在使用Perl 5.10.1

How can I check if the integer value 80 or 77 or 99 is in the array? Is there a better way than just doing a separate smartmatch for each integer in the list? I'm using Perl 5.10.1

#!/usr/bin/env perl
use warnings;
use 5.012;

my @array = (7,101,80,22,42);
my @items = (77,81,99);
my $it = join '|', @items;
my $re = qr/^(?:$it)\z/; 
say $re ~~ @array ? 'OK' : 'Not OK';