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?
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
}
Smart::Match operates like a "Util" module. It also has a none function where you would do: if ( $x ~~ none (@array) ) {say "not here so do stuff"}. It is almost as if it were acting like a Match::Util enhancement for a future (much simplified) version of the ~~ operator. Adding Smart::Match as a response.List::MoreUtils. If you add to that the dereferencing of references then you pretty much have it covered, and each different operation has a different name as well, so you're not writing $thing1 MAGIC $thing2 everywhere. Even object-oriented code can be problematic with such things, and that is about as far as I want to go$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.)
grep for this: metacpan.org/pod/release/THALJEF/Perl-Critic-1.125/lib/Perl/…grep or List::MoreUtils::none, pick your poison.none doesn't necessarily iterate through every single element, it stops after the first match, just like any.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')
grep for this: metacpan.org/pod/release/THALJEF/Perl-Critic-1.125/lib/Perl/…grep compares all elements of the list, regardless of whether an earlier match could short-circuit the process. As long as this is borne in mind, grep is a fine way of doing this, especially since the list is only two items in length, and the extra computing would be insignificant in any code that also reads data from a disk fileBuilding 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.
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";
}
grep for this: metacpan.org/pod/release/THALJEF/Perl-Critic-1.125/lib/Perl/…You can also use the smart match operator:
unless( $x ~~ ['value1','value2'] )
~~" if/when it goes away. There's always match::simple by @tobyink.~~ in future versions of Perl - the behavior of which could be enhanced and extended with a utility module and the CPAN ecosystem. /me crosses fingersYour 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")
@array somewhere which the value of $a is matching against. It's not the clearest example code but the OP should be the one to change it.