如何检查 Perl 数组是否包含特定值? [英] How can I check if a Perl array contains a particular value?

查看:39
本文介绍了如何检查 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.

推荐答案

只需将数组转成哈希即可:

Simply turn the array into a hash:

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

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

您还可以向列表中添加更多(唯一的)参数:

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

$params{$newparam} = 1;

然后返回一个(唯一的)参数列表:

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

@badparams = keys %params;

这篇关于如何检查 Perl 数组是否包含特定值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆