我如何检查是否一个Perl数组包含一个特定的值?

我如何检查是否一个Perl数组包含一个特定的值?

问题描述:

我试图找出检查数组中的一个值的存在,而不通过遍历数组的一种方式。

I am trying to figure out a way of checking for the existence of a value in an array without iterating through the array.

我读了一个参数的文件。我有一个参数一长串我不想去处理。我把这些多余的参数数组中的 @badparams

I am reading a file for a parameter. I have a long list of parameters I do not want to deal with. I placed these unwanted parameters in an array @badparams.

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

I want to read a new parameter and if it does not exist in @badparams, process it. If it does exist in @badparams, go to the next read.

只需打开数组的哈希:

my %params = map { $_ => 1 } @badparams;

if(exists($params{$someparam})) { ... }

您还可以添加更多的(唯一的)PARAMS到列表:

You can also add more (unique) params to the list:

$params{$newparam} = 1;

和后来得到(唯一)的列表PARAMS回:

And later get a list of (unique) params back:

@badparams = keys %params;