简体   繁体   English

在Perl中,如何检查数组是否至少匹配一次其内容中列出的值?

[英]In Perl, how do I check to see if an array matches a value listed at least once within it's contents?

I've never really worked with Perl before (avoided it where I could) and so my knowledge is sparse on the topic. 我以前从未真正与Perl合作过(尽可能避免使用Perl),因此我对该主题的知识很少。

I know the script I am looking at has an array of ProductID values in @::s_Ship_sShipProducts. 我知道我正在查看的脚本在@ :: s_Ship_sShipProducts中具有一个ProductID值数组。

I was attempting to see if any of the ProductIDs in the array began with either B or S and execute a function if so, else do another function. 我试图查看数组中的任何ProductID是否以B或S开头,如果是则执行一个函数,否则执行另一个函数。 But what I ended up with was for each ProductID do the statement. 但是我最终要为每个ProductID执行该语句。 This is what I (admittedly salvaged) had. 这就是我(公认打捞)的东西。

my $i;
for $i (0 .. $#::s_Ship_sShipProducts) # for each product
    if  ($::s_Ship_sShipProducts[$i] =~ /^(B|S)/) # Determine if B or S product
        {
        if (defined $phashBandDefinition->{'FreeOver'} && CalculatePrice() > 250)
        {$nCost = 0;}
        }
    else    {
        if (defined $phashBandDefinition->{'FreeOver'} && CalculatePrice() > $phashBandDefinition->{'FreeOver'})
        {$nCost = 0;}
        }

How could I change this so that the array was inspected to see if any ProductID was true and return true, or false if nothing matched? 我如何更改此值,以便检查数组以查看是否有任何ProductID为true并返回true,或者如果没有匹配项则返回false? Then execute the appropriate function based on true or false? 然后基于true或false执行适当的功能? I have done some reading, but am still in the dark. 我已经读了一些书,但仍然处于黑暗中。 Thanks for your time. 谢谢你的时间。

If the array is not too big (or you don't care THAT much about performance), you can check if there are any matching values via grep : 如果数组不是太大(或者您不太在乎性能),则可以通过grep检查是否存在任何匹配的值:

if (my @matching_elements = grep { /^(B|S)/ } @::s_Ship_sShipProducts) {
   # Better yet use /^[BS]/ expression - more idiomatic/readable
   print "Found B/S\n";
} else {
   print "NOT Found B/S\n";
}

When done, @matching_elements would contain a list of matching IDs. 完成后, @matching_elements将包含匹配ID的列表。

In the rare case when the array IS too big to scan through entirely and you only need to find the first occurance, you can use any of the array search optimization strategies discussed in binary search in an array in Perl 极少数情况下,如果数组太大而无法完全扫描,而您只需要查找第一个出现的数组,则可以在Perl中使用二进制搜索中讨论的任何数组搜索优化策略


By the way, your approach to search works perfectly fine, you merely need to quit the loop once you found - and to boot, is one of those optimized approaches shown in the list above: 顺便说一句,您的搜索方法可以很好地工作,只需找到后退出循环,然后启动即可,这是上面列表中显示的优化方法之一:

for my $i (0 .. $#::s_Ship_sShipProducts) # for each product
    if  ($::s_Ship_sShipProducts[$i] =~ /^[BS]/) {  # Determine if B or S product
        # Execute your logic here
        last; # Found, get out of the loop.
    } 
}

NOTE: In Perl 5.10 and above, you can - instead of grep - use a so-called "smart match" operator ~~ : 注意:在Perl 5.10及更高版本中,您可以-而不是grep使用所谓的“智能匹配”运算符~~

if (@::s_Ship_sShipProducts ~~ /^(B|S)/) {
    # Your logic
}

我不确定是否要关注您的问题,但是我认为您正在寻找grep

my @matched_products = grep(/^(B|S)/,@::s_Ship_sShipProducts);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查单击按钮的答案以查看它是否与 JavaScript 中的一组对象中找到的正确答案相匹配,以进行琐事游戏 - How do I check the answer of a clicked button to see if it matches the correct answer found within an array of objects in JavaScript for a trivia game 如何检查模型数组的内容是否包含在具有重复项且没有特定顺序的另一个数组的内容中 - How to check to see if a model array's contents are contained within another array's contents with duplicates and without a particular order 如何检查数组中是否有数组? - How to check to see if there's an array within an array? 如何检查数组的数组在内部数组中是否有值? - How do I check to see if an array of arrays has a value within the inner arrays? 如何检查子数组(数组内)是否匹配一个值? - How to check if subarray (within array) matches a value? 如何检查单元格的内容是否为数组中的值-VBA Excel - How do I check if the contents of a cell is a value in an array - VBA Excel 如何检查用户输入是否与二维数组的元素匹配? JAVA - How do I check to see if a user Input matches an element of a 2D array? JAVA 简单数组:检查值是否匹配 - Simple Array: Check to see if value matches Perl:查找变量的值是否与数组中的值匹配 - Perl: find if a variable's value matches that in an array 如何检查数组的内容? - How do I check the contents of an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM