Hi,
I’m a complete novice when it comes to programming and am totally stumped.
Hope someone could enlighten me about this two lines of code.
piece.m = type ~= table1.element1
state = (not v1) and table2.element1 or table2.element2
Thanks.
Hi,
I’m a complete novice when it comes to programming and am totally stumped.
Hope someone could enlighten me about this two lines of code.
piece.m = type ~= table1.element1
state = (not v1) and table2.element1 or table2.element2
Thanks.
Lua allows you to have conditional tests on the right hand side of the assignment equal sign with the result stored in the variable on the left hand side.
In the first one it compares the value of type and table1.element1 and if they are not equal, true is stored in piece.m. If they are not equal, false is stored.
The second one is pretty much the same thing. Lua evaluates left to right for truth in conditional tests and once it finds its first false, it stop. So in this case (not v1) determines if v1 is true or not (not nil, not false), if that’s true, then it moves to check table2.element1, if its not nil or not false, it will resolve to true. Since there is an “and” condition, at this point, if both (not v1) and table2.element1 have resolved to true, the last or condition will be tested for true false. Or conditions allow either side to be true and evaluate to be true or in other words, both have to be false to be false. That’s evaluated you either have a true or false condition that’s saved into the variable “state”.
Rob
This is so well-explained. And quick too. Thank you.
Lua allows you to have conditional tests on the right hand side of the assignment equal sign with the result stored in the variable on the left hand side.
In the first one it compares the value of type and table1.element1 and if they are not equal, true is stored in piece.m. If they are not equal, false is stored.
The second one is pretty much the same thing. Lua evaluates left to right for truth in conditional tests and once it finds its first false, it stop. So in this case (not v1) determines if v1 is true or not (not nil, not false), if that’s true, then it moves to check table2.element1, if its not nil or not false, it will resolve to true. Since there is an “and” condition, at this point, if both (not v1) and table2.element1 have resolved to true, the last or condition will be tested for true false. Or conditions allow either side to be true and evaluate to be true or in other words, both have to be false to be false. That’s evaluated you either have a true or false condition that’s saved into the variable “state”.
Rob
This is so well-explained. And quick too. Thank you.