我试图找出一种方法来检查数组中值的存在,而不遍历数组。

我正在读取一个文件的参数。我有一长串不想处理的参数。我把这些不需要的参数放在一个数组@badparams中。

我想读取一个新的参数,如果它不存在于@badparams,处理它。如果@badparams中存在,请转到下一次读取。


当前回答

这里肯定需要一个哈希。将坏的参数作为键放在散列中,然后决定某个特定参数是否存在于散列中。

our %bad_params = map { $_ => 1 } qw(badparam1 badparam2 badparam3)

if ($bad_params{$new_param}) {
  print "That is a bad parameter\n";
}

如果您真的对数组感兴趣,请查看List::Util或List::MoreUtils

其他回答

你可以在Perl 5.10中使用smartmatch特性,如下所示:

对于文字值查找,执行下面的操作即可。

if ( "value" ~~ @array ) 

对于标量查找,执行以下操作将与上述工作一样。

if ($val ~~ @array)

对于内联数组做下面,将工作如上。

if ( $var ~~ ['bar', 'value', 'foo'] ) 

在Perl 5.18中,smartmatch被标记为实验性的,因此你需要通过在你的脚本/模块中添加以下内容来打开实验性的pragma来关闭警告:

use experimental 'smartmatch';

或者,如果你想避免使用smartmatch,那么就像Aaron说的那样使用:

if ( grep( /^$value$/, @array ) ) {
  #TODO:
}

有两种方法。您可以使用将值扔到查找表的散列中,正如其他文章所建议的那样。(我再加一个成语。)

my %bad_param_lookup;
@bad_param_lookup{ @bad_params } = ( 1 ) x @bad_params;

但如果它的数据主要是单词字符,没有太多元,你可以把它转储到一个正则表达式的交替:

use English qw<$LIST_SEPARATOR>;

my $regex_str = do { 
    local $LIST_SEPARATOR = '|';
    "(?:@bad_params)";
 };

 # $front_delim and $back_delim being any characters that come before and after. 
 my $regex = qr/$front_delim$regex_str$back_delim/;

这个解决方案必须针对您正在寻找的“坏值”类型进行调优。这对于某些类型的字符串来说可能是完全不合适的,买者自负。

@eakssjo的基准测试被打破了——测量在循环中创建哈希和在循环中创建正则表达式。修正版本(加上我已经添加了List::Util::first和List::MoreUtils::any):

use List::Util qw(first);
use List::MoreUtils qw(any);
use Benchmark;

my @list = ( 1..10_000 );
my $hit = 5_000;
my $hit_regex = qr/^$hit$/; # precompute regex
my %params;
$params{$_} = 1 for @list;  # precompute hash
timethese(
    100_000, {
        'any' => sub {
            die unless ( any { $hit_regex } @list );
        },
        'first' => sub {
            die unless ( first { $hit_regex } @list );
        },
        'grep' => sub {
            die unless ( grep { $hit_regex } @list );
        },
        'hash' => sub {
            die unless ( $params{$hit} );
        },
    });

结果(100_000次迭代,比@eakssjo的答案多十倍):

Benchmark: timing 100000 iterations of any, first, grep, hash...
       any:  0 wallclock secs ( 0.67 usr +  0.00 sys =  0.67 CPU) @ 149253.73/s (n=100000)
     first:  1 wallclock secs ( 0.63 usr +  0.01 sys =  0.64 CPU) @ 156250.00/s (n=100000)
      grep: 42 wallclock secs (41.95 usr +  0.08 sys = 42.03 CPU) @ 2379.25/s (n=100000)
      hash:  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU) @ 10000000.00/s (n=100000)
            (warning: too few iterations for a reliable count)

尽管使用起来很方便,但转换为哈希的解决方案似乎消耗了相当多的性能,这对我来说是个问题。

#!/usr/bin/perl
use Benchmark;
my @list;
for (1..10_000) {
    push @list, $_;
}

timethese(10000, {
  'grep'    => sub {
            if ( grep(/^5000$/o, @list) ) {
                # code
            }
        },
  'hash'    => sub {
            my %params = map { $_ => 1 } @list;
            if ( exists($params{5000}) ) {
                # code
            }
        },
});

基准测试输出:

Benchmark: timing 10000 iterations of grep, hash...
          grep:  8 wallclock secs ( 7.95 usr +  0.00 sys =  7.95 CPU) @ 1257.86/s (n=10000)
          hash: 50 wallclock secs (49.68 usr +  0.01 sys = 49.69 CPU) @ 201.25/s (n=10000)

这篇博文讨论了这个问题的最佳答案。

作为一个简短的总结,如果你可以安装CPAN模块,那么最有可读性的解决方案是:

any(@ingredients) eq 'flour';

or

@ingredients->contains('flour');

然而,更常见的习语是:

any { $_ eq 'flour' } @ingredients

但是请不要使用first()函数!它根本没有表达你代码的意图。不要使用~~“智能匹配”操作符:它已经坏了。不要使用grep()或带散列的解决方案:它们遍历整个列表。

Any()一旦找到你的值就会停止。

查看博客文章了解更多细节。