rsriram has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I want to check whether a variable is equal to any one of the three (or even more) values. For example, I want to check whether $country equals to India, America, Germany or Austria. When I used if statement, in the following way, I did not get the output.
if($country eq (India|America|Germany|Austria)) { print "True"; }
Alternatively, if I use it the following way, it is working fine. But, this will practically be impossible for the number of options I am going to use in the script.
if($country eq "India"|$country eq "America"|$country eq "Germany"|$co +untry eq "Austria") { print "True"; }
If I use a array to store all the possible options, and check whether the variable $country matches to any of the elements, that too is working fine, but I am sure there will be a way of doing this in a simplified manner using if statement or any other conditional loops. Though it sounds simple, I have scratched my head enough. Can anyone help me please..
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using the 'if' statement...
by ikegami (Patriarch) on Jul 09, 2008 at 09:22 UTC | |
|
Re: Using the 'if' statement (with Benchmark)
by moritz (Cardinal) on Jul 09, 2008 at 10:08 UTC | |
|
Re: Using the 'if' statement...
by cdarke (Prior) on Jul 09, 2008 at 09:26 UTC | |
by ikegami (Patriarch) on Jul 09, 2008 at 10:02 UTC | |
by harishnuti (Beadle) on Jul 09, 2008 at 09:44 UTC | |
by ikegami (Patriarch) on Jul 09, 2008 at 10:05 UTC | |
by DrHyde (Prior) on Jul 09, 2008 at 10:29 UTC | |
|
Re: Using the 'if' statement...
by toolic (Bishop) on Jul 09, 2008 at 13:25 UTC |