Perl 5.10 or higher?
use strict; use warnings; use 5.10.0; my @fruits_i_like = qw/mango banana apple/; my $this_fruit = 'banana'; if ( $this_fruit ~~ \@fruits_i_like ) { say "yummy, I like $this_fruit!"; }
Until 5.10:
use strict; use warnings; my @fruits_i_like = qw/mango banana apple/; my $this_fruit = 'banana'; if ( scalar grep $this_fruit eq $_, @fruits_i_like ) { print "yummy, I like $this_fruit!\n"; }
The downside is that the entire array is parsed to find matches. This may not be the best option, in which case you can use List::MoreUtils ' any() , which returns true when it matches the value and does not continue to pass through the array.
use strict; use warnings; use List::MoreUtils qw/any/; my @fruits_i_like = qw/mango banana apple/; my $this_fruit = 'banana'; if ( any { $this_fruit eq $_ } @fruits_i_like ) { print "yummy, I like $this_fruit!\n"; }
Happy hack!
mfontani
source share