首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在perl数组和子字符串中存在检查值。

在perl数组和子字符串中存在检查值。
EN

Stack Overflow用户
提问于 2017-05-30 10:31:37
回答 1查看 497关注 0票数 0

我有一个包含原始数据的数组和一个excel文件中的列,我需要比较excel列和数组并找到匹配。

我需要的是走路应该和我一起走等等,有什么办法我能做到吗?

代码语言:javascript
复制
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>"
                }

      }
    }

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-30 16:31:29

将列表分配给标量($db_description)没有任何意义。另外,eq测试字符串相等性,您要比较的字符串中没有一个是相等的。您可能希望使用正则表达式来查看针头是否与干草堆的一部分匹配。

代码语言:javascript
复制
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";
    }
}

这可以缩短为:

代码语言:javascript
复制
for my $needle (@needles) {
    if (grep { /\Q$needle\E/ } @haystacks) {
        print "Found [$needle]\n";
    } else {
        print "Couldn't find [$needle] anywhere!\n";
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44259793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档