检查特定值不在数组中 [英] Check specific value not in array

查看:176
本文介绍了检查特定值不在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组有两个值,如果输入不在该数组,需要执行一些操作。



我试过像

  if($ a ne('value1'||'value2')

if(($ a ne'value1')| |($ a ne'value2'))

https://metacpan.org/pod/List::MoreUtils> List :: MoreUtils



如果你真的有一个数组作为主题行,那么你的代码看起来像这样

 使用List :: MoreUtils'none'; 

if(none {$ _ eq $ a} @array){
#Do stuff
}

或者如果你真的有两个常数,那么你可以使用

  if(none {$ _ eq $ a}'value1','value2'){
#Do stuff
}

但在这种情况下,我宁愿只看到

  if $ a ne'value1'and $ a ne'value2'){
#Do stuff
}


I have an array with two values and need to perform some operations if input is not in that array.

I tried like

if ($a ne ('value1' || 'value2')

if (($a ne 'value1' ) || ($a ne 'value2' ))

Both methods didn't work. Can anyone please help?

解决方案

You could use the none function from List::MoreUtils.

If you really have an array as your subject line says then your code would look like this

use List::MoreUtils 'none';

if ( none { $_ eq $a } @array ) {
  # Do stuff
}

or if you really have two constants then you could use this

if ( none { $_ eq $a } 'value1', 'value2' ) {
  # Do stuff
}

but in this case I would prefer to see just

if ( $a ne 'value1' and $a ne 'value2' ) {
  # Do stuff
}

这篇关于检查特定值不在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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