I have a list of possible values:
@a = qw(foo bar baz);
How do I check in a concise way that a value $val is present or absent in @a?
An obvious implementation is to loop over the list, but I am sure TMTOWTDI.
Thanks to all who answered! The three answers I would like to highlight are:
-
The accepted answer – the most ‘built-in’ and backward-compatible way.
-
RET’s answer is the cleanest, but only good for Perl 5.10 and later.
-
draegtun’s answer is (possibly) a bit faster, but requires using an additional module. I do not like adding dependencies if I can avoid them, and in this case do not need the performance difference, but if you have a 1,000,000-element list you might want to give this answer a try.
Perl’s bulit in grep() function is designed to do this.
or you can insert any expression into the matcher