Hello Anant,

> I want to search whether a scalar '$a' contains any one of value of an
> array '@arr'.
> How it may be done?

If you are using perl v5.010 or later, you can use the smart match operator 
(~~):

use 5.010;

use strict;
use warnings;

my @array = qw( foo bar quux );
my $item = 'bar';

if ( $item ~~ @array ) {
    say "Item found: $item";
}

However, if you have an older perl, you can use List::Util's (which is a core 
module since perl v5.7.3) `first` subroutine:

use strict;
use warnings;

use List::Util qw( first );

my @array = qw( foo bar quux );
my $item = 'bar';

if ( first { $item eq $_ } @array ) {
    print "Item found: $item\n";
}

Regards,
Alan Haggai Alavi.
-- 
The difference makes the difference.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to