检查值存在于perl数组和子string中

我有一个原始数据和Excel文件中的列的数组,我需要比较Excel列和数组,并find匹配。

我需要的是散步应该与我一起走路等等,有没有什么办法可以做到。

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 )没有任何意义。 此外, eqtestingstring相等,并没有任何你比较的string将永远是平等的。 你可能想用一个正则expression式来看你的针是否与你的干草堆相匹配:

 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"; } }