Arrays 检查值存在于perl数组和子字符串中
我有一个包含原始数据的数组和excel文件中的一列,我需要将excel列与数组进行比较并找到匹配项 我需要的是散步,应该和我一起散步等等,我有什么办法可以做到吗Arrays 检查值存在于perl数组和子字符串中,arrays,excel,perl,Arrays,Excel,Perl,我有一个包含原始数据的数组和excel文件中的一列,我需要将excel列与数组进行比较并找到匹配项 我需要的是散步,应该和我一起散步等等,我有什么办法可以做到吗 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
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>"
}
}
}
我是否可以将单个字符串与完整数组进行比较我正在使用以下方法将excel行与数组进行比较description变量包含与@steps\u name数组匹配的字符串
我的@steps_name=(“1-2步”、“5-7步”、“8-10步”、“11-15步”);
foreach$sheet(@{$workbook->{sheet}){
foreach$col($sheet->{MinCol}..$sheet->{MaxCol})
{
if($sheet->{Cells}[0][$col]->{Val}eq“说明”)
{
$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\u description\E/}@steps\u name)
{
打印“
找到元素“$db_description””;
}
其他的
{
打印“
未找到$db\u说明””
}
}
}
提前感谢。将列表分配给标量(
$db\u description
)没有任何意义。另外,eq
测试字符串是否相等,并且您正在比较的所有字符串都不会相等。您可能希望使用正则表达式查看针是否与干草堆的一部分匹配:
这可以缩短为:
for my $needle (@needles) {
if (grep { /\Q$needle\E/ } @haystacks) {
print "Found [$needle]\n";
} else {
print "Couldn't find [$needle] anywhere!\n";
}
}
您已经知道如何读取Excel文件了吗?是的,我知道我正在使用foreach$row($sheet->{MinRow}+1..10){}您应该包含一些有关如何处理该文件以及该文件外观的更多信息。这太模糊了。我的基本要求是匹配上面的两个字符串。这是可行的,但区分大小写,不用它也可以。谢谢你的解决方案。@rohan也许你想对不区分大小写的匹配使用
/i
regex标志?$haystack=~/\i\Q$needle\E\i/这样使用,但不起作用。@rohan你只是在猜测语法吗?检查一下,应该会清楚需要做什么。@rohan您的/i
应用程序与几条评论之前的应用程序相同。你看过我链接到的perlre手册页面了吗?
for my $needle (@needles) {
if (grep { /\Q$needle\E/ } @haystacks) {
print "Found [$needle]\n";
} else {
print "Couldn't find [$needle] anywhere!\n";
}
}