In perl, what’s the best way of testing a variable against multiple values?
Something like this (in pseudo code):
if x is in {'q','w','e','r','t'}
# do something
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
How about:
This works if the values you are comparing are scalars (strings or numbers).
For strings, there is a nice shorthand:
If you don’t like the regex notation (
/^x$/), there is:grep {$_ eq ‘x’} qw(q w e r t y)
Where you can use
$_to test for anything, not just equality.If what you want to do is simple (can be expressed in a line), just this will do: