[英]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.