|
Alvar Newbie
Joined: 05 Jun 2007 Posts: 8
|
Posted: Tue Jun 12, 2007 7:02 pm
How to do comparison of values in #IF? |
I've got the following code snippet in a trigger that just won't do what I want.
Code: |
#say @nummade
#say @numwanted
#if {@numwanted <= @nummade}
{numwanted := 0;
nummade := 0;}
{mbh} |
The variables nummade and numwanted have the values 1 and 5 respectively, which is confirmed by the #say statements.
I would expect the condition for the #if statement to be false, so the string mbh should be sent to the MUD. Instead it jumps into the first set of commands and sets the variables back to 0. What am I doing wrong? |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Tue Jun 12, 2007 7:17 pm |
Expressions need to be surrounded with brackets () - commands with braces {}. So you want #if (@numwanted <= @nummade). Also, you don't need semicolons between your commands if you're using the multi-line syntax, and you never need them at the end of the list of commands like you have there. Your assignment syntax is also wrong - you don't need the colons, just an equals sign. I'm not sure if it'll matter, but the pretty printer always puts the opening brace of the true-command on the same line as the #if command, and then gives the closing bracket (and the opening bracket of the false-command) its own line. That may or may not make a difference.
|
|
|
|
Alvar Newbie
Joined: 05 Jun 2007 Posts: 8
|
Posted: Tue Jun 12, 2007 7:20 pm |
Hmm. Removing the semicolons gives me a "illegal character in expression: =" error message.
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Tue Jun 12, 2007 7:28 pm |
I don't know what to say other than that it's most definitely wrong and I can't get it to fail just by removing the colons from your code above.
|
|
|
|
Alvar Newbie
Joined: 05 Jun 2007 Posts: 8
|
Posted: Tue Jun 12, 2007 7:54 pm |
Okay, using the parentheses works.
The colons in the assignments don't make a difference, but according to the help file they're allowed as an alternative syntax and I prefer to make a clear distinction between assignments and comparisons.
What I tried to remove was the semicolons between the statements. That brought the compile error. |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Wed Jun 13, 2007 4:14 pm |
Here is the correct syntax for your script:
Code: |
#say @nummade
#say @numwanted
#if (@numwanted <= @nummade) {
numwanted := 0
nummade := 0
} {mbh} |
That uses the preferred multiline syntax. In your original example, your "nummade := 0" line was indented one space more than the line above. That made it into a "continuation" of the previous line. Since you made this a continuation line, the semicolon was required to separate the two commands. With proper indentation you don't need the semicolon.
CMUD requires you to be much more careful about proper indentation and syntax, just like most other compiled languages. |
|
|
|
|
|