Check specific value not in array


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 <a href="https://metacpan.org/pod/List::MoreUtils" rel="nofollow">List::MoreUtils</a>.

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 }


$a is not in the array if it's different to the first element <strong>and</strong> 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 <a href="http://p3rl.org/perlvar" rel="nofollow">perlvar</a>.)


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

evaluates to

if ($a ne 'value1')


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

is always TRUE.

You might try

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


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


Building on the smartmatch solution by @Dilbertino (nice nick) using <a href="https://metacpan.org/pod/match::simple" rel="nofollow">match::simple</a> 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.

<a href="https://metacpan.org/pod/Smart::Match" rel="nofollow">Smart::Match</a> also has a none function. To use it you would do:

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


Discussion here on Stackoverlfow (see: <a href="https://stackoverflow.com/questions/16927024/perl-5-20-and-the-fate-of-smart-matching-and-given-when" rel="nofollow">Perl 5.20 and the fate of smart matching and given-when?</a>) and elsewhere (<em>c.f.</em> the <a href="http://www.perlmonks.org/?node_id=1039472" rel="nofollow">Perlmonks article</a> by @ikegami from circa perl-5.18) gives the context for the smartmatch experiment. <strong>TLDR;</strong> things might change in the future but meanwhile, you can go <strong>back in time</strong> 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 <a href="https://metacpan.org/pod/Smart::Match" rel="nofollow">Smart::Match</a> (<em>i.e</em>. 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.


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"; }


You can also use the smart match operator:


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



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";


@array = qw/X Y/;

Condition check using if:

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


  • Is there a dedicated function to check null and undefined in TypeScript?
  • show message when no rows selected mysql php
  • NodeJS Sendrgrid Issue in sending email to multiple recepients
  • error 400 bad request with XEP-0055?
  • Angular/Nativescript: Listview doesn't show at all
  • JButton background images
  • Javascript Unit test
  • How do I get the data from a finished `NSURLSessionDataTask`?
  • CSS - Cursor Transition
  • Functions returning a const value [duplicate]
  • named parameters in sp_executesql
  • Installing Flash Player on an Android Emulator
  • Get highest value from a file using mSL and mIRC
  • Adding millions of nodes to neo4j spatial layer using cypher and apoc
  • Zend Framework bassed projects
  • ZipList with Scalaz
  • Unable to install Git-core+svn by MacPorts
  • How do I superscript characters in a UIButton?
  • Django simple Captcha “No module named fields” error
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • Could not find rake using whenever rails
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • What is Eclipse's Declaration View used for?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Align navbar back button on right side
  • HTML download movie download link
  • Modifying destination and filename of gulp-svg-sprite
  • How to handle AllServersUnavailable Exception
  • How to model a transition system with SPIN
  • VBA Convert delimiter text file to Excel
  • ORA-29908: missing primary invocation for ancillary operator
  • How to disable jQuery.jplayer autoplay?
  • How to stop GridView from loading again when I press back button?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?