Forum Moderators: coopster & phranque

Message Too Old, No Replies

It's executing else in addition to the if()s!

Why?

         

adni18

4:00 am on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a variation of my script, but it's the same basic problem. I have a script, with more than 1 if() statement. I think perl is grouping the last if statement with the else statement. Try this on your server:

--------------------------

#!/usr/bin/perl

use CGI;

print "Content-type:text/html\n\n";

$str="phi";

if($str eq "phi")
{
print "Phi. ";
}

if($str eq "phiq")
{
print "Phiq! ";
}

else
{
print "OTHERWISE! UNFORTUNATELY, Phi. is printed to the left of this!";
}

willybfriendly

4:29 am on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is doing exactly what you told it to do.

if(condition)
{
code block
}
else if(condition)
{
code block
}
else if(condition)
{
code block
}
else(condition)
{
code block
}

Might work for you. I am not clear what you are trying to accomplish. Else only exists in conjunction with an if statement, so of course it is going to be joined with the last if the way you have it written.

WBF

xunker

4:45 am on Oct 23, 2004 (gmt 0)

10+ Year Member



If I had to field a guess, I'd say you need change your second "if" to an "elsif", something like this:


if($str eq "phi")
{
print "Phi. ";
}
elsif($str eq "phiq")
{
print "Phiq! ";
}
else
{
print "OTHERWISE! UNFORTUNATELY, Phi. is printed to the left of this!";
}

..this will group both "if" tests together so that if _both_ the first and second tests (both of the "ifs") are not true (not equal) only then will the "else" clause be executed. As you had it before the "else" only applied to the second "if".

adni18

1:56 pm on Oct 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, guys. I had tried the elsif module before, but I had put elseif instead of elsif. Thanks!