Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ function return and a simple if statement
|
function return and a simple if statement
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1
2
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #15 on Sun 04 Mar 2007 04:26 AM (UTC) |
| Message
|
Quote:
if string.find (defs, "p") ~= nil then aff.prone = 1 else aff.prone = 0 end
Apart from the problem with 0 and 1, you can rewrite that more neatly:
aff.prone = string.find (defs, "p") ~= nil
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Gore
(207 posts) Bio
|
| Date
| Reply #16 on Mon 05 Mar 2007 08:49 AM (UTC) |
| Message
| | I'm assuming that it sets the variable to true or false, but how exactly does it work? | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #17 on Mon 05 Mar 2007 09:24 AM (UTC) |
| Message
| The first thing that happens is figuring out the value of the following:
string.find (defs, "p") ~= nil
For simplicity's sake, let's call the result of string.find "result".
If result is equal to nil, then result == nil is true, so result ~= nil is false. Therefore, the whole expression is false, so we put false into aff.prone.
However if result is not nil, then result == nil is false so result ~= nil is true. So the expression is true, and aff.prone becomes true.
In general, you have:
variable = expression
So, you first figure out the value of expression, and you put it into variable.
Personally I would have written:
aff.prone = (string.find (defs, "p") ~= nil)
To me, adding in the parentheses makes the assignment clearer. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Gore
(207 posts) Bio
|
| Date
| Reply #18 on Mon 05 Mar 2007 10:53 AM (UTC) |
| Message
| I thought that was what was happening, but I didn't want to assume.
That's a very neat shortcut, thanks a lot -g-
I wonder what other shortcuts are nice to use. Lua is amazing! | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #19 on Mon 05 Mar 2007 11:07 AM (UTC) |
| Message
| Actually it's a fairly common trick across languages. Almost all languages assign an expression to a value (in some, the assignment itself is an expression!). Then, any kind of expression is fair game.
Syntactically, the number "4" alone is an expression, and so is, say, "4+2". But, you can also have relational operators: things that compare two values and return true if the values are in that relation. You could have, say in C++:
bool b = 1 < 2;
This would be always true.
Once you accept this, it's just one small step to allow for function calls as parts of that expression. And then you can have things like what Nick showed you, or arbitrarily complicated expressions the value of which will be assigned to a variable.
Consider this, in C(++):
int i;
int j;
i = (j = (5>3 ? 24 : 5));
This is somewhat contrived, but what this means is this:
If 5>3, assign 24 to j; otherwise assign 5.
Assign to i the value of j's assignment. (In other words, assign j to i.)
One common thing to see is something like this:
char * p;
if ( (p = get_the_pointer()) != NULL ) {
// do something interesting with p
}
In this case, we first get a value for p. Then, we check p's value against NULL; if it's not null, we do whatever's inside the if block, with a value already assigned to p. It is shorter than, but basically the same as, the following:
if ( get_the_pointer() != NULL ) {
char * p = get_the_pointer();
// do something interesting with p
}
(It's not the same thing when the function get_the_pointer has side effects.) |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Gore
(207 posts) Bio
|
| Date
| Reply #20 on Tue 20 Mar 2007 09:04 PM (UTC) |
| Message
| | Sweet, thanks a bunch :> | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
74,597 views.
This is page 2, subject is 2 pages long:
1
2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top