Perl 检查标量是否包含数组中的元素之一 [英] Perl Checking if a scalar contains one of the elements in an array
本文介绍了Perl 检查标量是否包含数组中的元素之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组
my @array = qw/FOO BAR BAZ/;
和从包含数据的文件中读取的标量
and a scalar read from a file containing data like
+++123++585+++FOO
或
+++589++458+++XYZ
我正在寻找一种很好的方法来检查数组的元素是否与输入字符串的一部分匹配.
I am looking for a nice way to check if an element of the array matches part of the input string.
我知道我可以循环遍历数组并以这种方式进行匹配,但想知道是否有更像 perl 的方式.
I know I could just loop over the array and match that way but was wondering if there is a more perl like way.
推荐答案
您可以创建一个匹配所有@array 的正则表达式:
You can create a regex that matches all of the @array:
my $regex = join '|', map quotemeta, @array;
$string =~ $regex;
这篇关于Perl 检查标量是否包含数组中的元素之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文