Gossamer Forum
Home : General : Perl Programming :

Help with "If"

Quote Reply
Help with "If"
I am trying to print some text to the screen depending on if variables equal each other - if not then print something else. The following is the piece of code I am having the problem with:

if ($date eq $caldate) {
print "<td align=\"right\"><font face=\"arial\" COLOR=\"#000000\" size=\"1\">";
print "$First_Name $Last_Name</font></td></tr>\n";
}
else {
print "<td align=\"right\"><font face=\"arial\" COLOR=\"#000000\" size=\"1\">";
print "None Today</font></td></tr>\n";
}

The "else" does not print when it is supposed to - any ideas as to why?

I have tried this as the "else" also:

else if ($date ne $caldate) {
print "<td align=\"right\"><font face=\"arial\" COLOR=\"#000000\" size=\"1\">";
print "None Today</font></td></tr>\n";
}

Any ideas on how I can get the "else" to print when $date and $caldate is not equal to each other?

TIA,

----------------
donm

Quote Reply
Re: Help with "If" In reply to
1) You need to use elsif NOT else if.
2) I would check to make sure that the $caldate is correctly configured and also that you are properly referencing the $date variable.

Regards,

Eliot Lee

Quote Reply
Re: Help with "If" In reply to
In regards to 1) No he doesn't. The if-else is perfectly valid. "If the condition exists, to this, otherwise just do this". Elsif is when there are more options: "If the condidtion exists, do this, otherwise if THIS condition exists, do this, otherwise just do this". You should never have an if-elsif with no else clause in there. It's considered bad form.

Don:

As Eliot said, you need to check your data. If the "if" is always executing, then the variables are equal.

Before the if statement, add in a print "date => $date caldate => $caldate"; to see the values in your variables.

Also, no need to duplicate HTML that prints out regardless of which branch gets executed. restructure like this:

Code:
print qq~<td align="right"><font face="arial" COLOR="#000000" size="1">~;
if ($date eq $caldate) {
print "$First_Name $Last_Name\n";
}
else {
print "None Today\n";
}
print '</font></td></tr>';
--mark


Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Help with "If" In reply to
I think Eliot meant that the person had used else if instead of elsif, referring to syntax, not whether an elsif was acutally needed.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Help with "If" In reply to
Yes...exactly...that was what I was referring to...not to the logic of the codes, but the syntax...

Thank you, Alex.

Regards,

Eliot Lee

Quote Reply
Re: Help with "If" In reply to
Oops, my bad! Didn't comprehend the second paragraph. Sorry Eliot!

--mark



Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: Help with "If" In reply to
Just wanted to thanks for your help to all who responded - however I found that it was not actually a problem with the "if" statement but rather with another part of the script - thanks again for your help !

-----------------
donm