Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using IF to check multiple conditions?

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi Folks

I've got a variable in my Perl script called $config. I want to ensure that it's value is equal to either 1, 2, or 3. What I'd really like to do is this:

IF $config IS NOT EQUAL TO 1, 2 OR 3
print "Variable is not valid!";

I can't seem to figure out a way to do this in two lines though. The best I can manage is:

if ($config == 1) {
print "Variable is equal to 1";
} elsif ($config == 2) {
print "Variable is equal to 2";
} elsif ($config == 3) {
print "Variable is equal to 3";
} else {
print "Variable is not valid!";
}

Is there any other way I can do this or is my code above the best solution?

Cheers,
Kenny.
 
You can use the logical 'and' operator && to combine conditionals. Use || for 'or'.

Code:
if($config != 1 && $config != 2 && $config != 3)
{
  print "Variable is not valid!";
}
----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Another way to check is:
Code:
if ( $config =~ /^[123]$/ ) {
jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top