mayank2211 has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a requirement in perl where i want to verify that the input value matches from any of the values in a array variable. can you please suggest how to achieve this. Please Find Below a sample code.

my $input_val = 'abc'; @names = ("abc", "def", "ijk"); if($input_val eq @names) {print "Match Found";} else { print "Match Failed";}

Replies are listed 'Best First'.
Re: if Loop with array
by toolic (Bishop) on Jun 11, 2015 at 16:20 UTC
    grep is one way:
    use warnings; use strict; my $input_val = 'abc'; my @names = ("abc", "def", "ijk"); if (grep { $input_val eq $_ } @names) {print "Match Found";} else { print "Match Failed";} print "\n";

    See also (for potentially faster look-ups):

      Thanks. Your suggestion solved the issue. Thanks a lot
Re: if Loop with array
by ikegami (Patriarch) on Jun 11, 2015 at 16:19 UTC
    if ( grep { $_ eq $input } @names ) { ... }