Perl:如何检查整数列表中是否有任何整数在数组中 [英] Perl: How to check if any integers in a list of integers are in an array

查看:70
本文介绍了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';

这篇关于Perl:如何检查整数列表中是否有任何整数在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆