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

发布于 2024-12-04 19:02:51 字数 826 浏览 1 评论 0 原文

我以前从未真正使用过 Perl(尽可能避免使用),因此我对这个主题的了解很少。

我知道我正在查看的脚本在 @::s_Ship_sShipProducts 中有一个 ProductID 值数组。

我试图查看数组中是否有任何 ProductID 以 B 或 S 开头,如果是,则执行一个函数,否则执行另一个函数。但我最终得到的是为每个 ProductID 执行该语句。这就是我(诚然挽救了)所拥有的。

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

我如何更改此设置,以便检查数组以查看是否有任何 ProductID 为 true 并返回 true,如果没有匹配则返回 false?然后根据true或false执行相应的函数?我读了一些书,但仍然一无所知。感谢您抽出时间。

I've never really worked with Perl before (avoided it where I could) and so my knowledge is sparse on the topic.

I know the script I am looking at has an array of ProductID values in @::s_Ship_sShipProducts.

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. But what I ended up with was for each ProductID do the statement. 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? Then execute the appropriate function based on true or false? I have done some reading, but am still in the dark. Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-12-11 19:02:51

如果数组不是太大(或者你不太关心性能),你可以通过 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";
}

完成后,@matching_elements 将包含匹配 ID 的列表。

罕见情况下,当数组太大而无法完全扫描而您只需要查找第一次出现时,您可以使用Perl 数组中的二进制搜索


顺便说一句,你的方法搜索工作完全正常,您只需要在找到后退出循环 - 并且启动,是上面列表中显示的优化方法之一:

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.
    } 
}

注意:在 Perl 5.10 及更高版本中,您可以 - 而不是 grep - 使用所谓的“智能匹配”运算符~~

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

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:

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.

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


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 ~~:

if (@::s_Ship_sShipProducts ~~ /^(B|S)/) {
    # Your logic
}
焚却相思 2024-12-11 19:02:51

我不太确定我是否在关注您的问题,但我认为您正在寻找 grep< /a>.

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

I'm not quite sure if I'm following your question, but I think you are looking for grep.

my @matched_products = grep(/^(B|S)/,@::s_Ship_sShipProducts);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文