检查值存在于perl数组和子字符串中 [英] check value exists in perl array and substring
本文介绍了检查值存在于perl数组和子字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有原始数据的数组,并且在excel文件中有一个列,我需要将excel列与该数组进行比较并找到匹配项。
I have an array with original data and a column in excel file, I need to compare the excel column with the array and find the match.
我需要的是步行应该与步行与我匹配,依此类推,有什么办法可以做到。
I need is walk should match with walk with me and so on, Is there any way I can do it.
can i compare individual string with complete array i am comparing excel row with array using the following way description variable contains string to match with @steps_name array
my @steps_name=("1-2 Steps", "5-7 Steps", "8-10 Steps", "11-15 Steps");
foreach $sheet (@{$workbook->{Worksheet}}) {
foreach $col ($sheet->{MinCol} .. $sheet->{MaxCol})
{
if ($sheet->{Cells}[0][$col]->{Val} eq "DESCRIPTION")
{
$description = $col;
}
}
foreach $row ($sheet->{MinRow}+1 .. 50)
{
my $db_description = $sheet->{Cells}[$row][$description]->{Val};
my $needle_regex = quotemeta $db_description;
if (grep { /(?i)\Q$db_description\E/ } @steps_name)
{
print "<br><h1>Element '$db_description' found </h1></br>" ;
}
else
{
print "<br>$db_description not found </br>"
}
}
}
感谢
推荐答案
将列表分配给标量( $ db_description
)没有任何意义。另外, eq
测试字符串是否相等,所比较的字符串将永远相等。您可能想使用正则表达式来查看您的针是否与您的干草堆的一部分匹配:
Assigning a list to a scalar ($db_description
) doesn't make any sense. Also, eq
tests for string equality, and none of the strings you're comparing will ever be equal. You probably want to use a regular expression to see if your needles match part of your haystacks:
use strict;
use warnings;
my @needles = ("walk", "run", "dance", "catch");
my @haystacks = ("walk with me", "come and run", "catch the ball");
for my $needle (@needles) {
my $found;
for my $haystack (@haystacks) {
if ($haystack =~ /\Q$needle\E/) {
print "Found [$needle] in [$haystack]\n";
$found++;
}
}
if (!$found) {
print "Couldn't find [$needle] anywhere!\n";
}
}
这可以缩短为:
for my $needle (@needles) {
if (grep { /\Q$needle\E/ } @haystacks) {
print "Found [$needle]\n";
} else {
print "Couldn't find [$needle] anywhere!\n";
}
}
这篇关于检查值存在于perl数组和子字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文