Check specific value not in array

喜你入骨 提交于 2020-01-06 02:12:24

问题


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?


回答1:


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
}



回答2:


$a is not in the array if it's different to the first element and it's different to the second one, too.

if ($x ne 'value1' and $x ne 'value2') {

For a real array of any size:

if (not grep $_ eq $x, @array) {

(I use $x instead of $a, as $a is special - see perlvar.)




回答3:


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

evaluates to

if ($a ne 'value1')

and

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

is always TRUE.

You might try

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

or

if (!grep{$a eq $_} 'value1', 'value2')



回答4:


Building on the smartmatch solution by @Dilbertino (nice nick) using match::simple by @tobyink to ease the pain of smartmatch going away (I miss it already):

use match::simple;
my @array = qw(abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt);
my $x="abcd.txt" ;
say "it's there" if ($x |M| \@array );

The |M| operator from match::simple can be replaced with a match function which speeds things up a bit (it is implemented with XS):

use match::simple qw(match);
my @array = qw(abcd.txt abcdeff.txt abcdweff.txt abcdefrgt.txt);
my $x="xyz.txt" ;

if ( match ( $x, \@array ) ) {
     say "it's there!" ;
}
else {
     say "no hay nada";
} 

It's "simple" because the RHS controls the behavior. With match::simple if you are matching against an array on the RHS it should be an arrayref.

Smart::Match also has a none function. To use it you would do:

if ( $x ~~ none (@array) ) { 
   say "not here so do stuff ..."; 
}

Appendix

Discussion here on Stackoverlfow (see: Perl 5.20 and the fate of smart matching and given-when?) and elsewhere (c.f. the Perlmonks article by @ikegami from circa perl-5.18) gives the context for the smartmatch experiment. TLDR; things might change in the future but meanwhile, you can go back in time and use match::smart qw(match); with perl-5.8.9 proving once again that perl never dies; it just returns to its ecosystem.

In the future something like Smart::Match (i.e. the non-core CPAN module not the concept) can help supercharge a simplified smart matching operator with helper functions that read like adverbs and adjectives and have the added bonus (as I understand it) of clarifying/simplifying things for perl itself since the ~~ operator will have a less ambiguous context for its operations.




回答5:


I would do something like this using grep with a regex match

#!/usr/bin/perl
use warnings;
use strict;

my @array = ('value1','value2');

if(grep(/\bvalue1\b|\bvalue2\b/, @array)){
    print "Not Found\n";
}
else {
    print "do something\n";
}



回答6:


You can also use the smart match operator:

unless( $x ~~ ['value1','value2'] )




回答7:


Your variable $a is not evaluated as a array without [INDEX] index, but is been treated as a scalar.

Two value array:

$array[0] = "X"; $array[1] = "Y";

or

@array = qw/X Y/;

Condition check using if:

if ( $array[0] ne "Your-String" || $array[1] ne "Your-String")



来源:https://stackoverflow.com/questions/29944441/check-specific-value-not-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!