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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use an Or in an if Statement?

Status
Not open for further replies.

Ati2ude

Programmer
Dec 11, 2001
79
US
I am new to Perl and am trying to modify a script. What I have is an if statement that is checking for a single condition like so:

if {$filename ne ".txt"} {$merr=1;$e2=1;}

What I would like to do is something like this:
if {$filename ne ".txt"} || {$filename ne ".TXT"} {$merr=1;$e2=1;}

When I try this I get this error:
syntax error at line 24, near "if {"

I hope that makes since, currently the script is over looking the upper case extensions. I am not looking to re-invent the wheel here, just add this case and move on. Thanks in advance for any pointers.

Brian
 
you need normal parens - not squiggly ones:-

if ( [red]condition || condition2[/red] ) {
# do this
}


Kind Regards
Duncan
 
Thanks for the quick reply! I did make the changes you suggested, but have one another question.

Do I leave spaces between the pipes and the rest of the statement?

I have pasted the actual code below for your review.

if ($ckfilename ne ".edi")||($ckfilename ne ".EDI") {
($merr=1;$e2=1;)
}

I am still getting two syntax errors with the changes:
One concerns the pipes and the other concerns a comment, I removed the comment to see if I could eliminate that one.

Brian
 
Ati2dude

No need for a double comparison.

$ckfilename = "file.edi";

unless ($ckfilename =~ /\.edi/i) {
my $merr=1;
my $e2=1;
print "Error \n";
}

 
Or if you prefer...

my $ckfilename = "file.edi";

if ($ckfilename !~ /\.edi/i) {
my $merr=1;
my $e2=1;
print "Error \n";
}
 
I am testing your first example currently, Thanks for the pointers. I do not claim to know perl but must support a few scripts to survive.

Thanks for the help, I will let you know the outcome.

Brian
 
Ati2ude

The reason you were getting an error is because you didn't quite do as i suggested. You must put the condition(s) within the parens - you can have conditions within parens also - but the overall syntax MUST be as follows:-

Code:
if ( $ckfilename eq "txt" || ($ckfilename eq "txt") || condition3 )


Kind Regards
Duncan
 
You can also ignore the capitalization without the regex engine by doing something like:
Code:
if (lc($filename) ne ".txt") { # do some stuff }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top