It is currently March 28th, 2024, 1:27 pm

[HELP] Lua "and"

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Help with Lua "and"

Post by jsmorley »

smurfier wrote:I understand your PM, I just wanted to hide my "duh" moment behind closed doors.

I just need to understand that particular like of shorthand because I feel really bad "publishing" a line of code that I don't understand. Also, I've been reading the tutorial, I'm just one that needs to learn things the hard way.
Yeah, I sorta am too... I think Yggdrasil might be best able to put that structure into "english" without mangling it.

In general though it is much like "MeasureTemp <= 33 ? 1 : 0" in a Calc measure. If the "conditional" evaluates to "true" it returns the first value, otherwise it returns the second.

From a different guide:

Another useful idiom is (a and b)or c (or simply a and b or c, because and
has a higher precedence than or), which is equivalent to the C expression a?b:c,
provided that b is not false. For instance, we can select the maximum of two
numbers x and y with a statement like

max = (x > y) and x or y

When x>y, the frst expression of the and is true, so the and results in its second
expression (x), which is always true (because it is a number), and then the or
expression results in the value of its frst expression, x. When x>y is false, the
and expression is false and so the or results in its second expression, which is y.

If you look at that last paragraph with your head tilted just slightly sideways, you can see why the "and" and "or" are used in the expression. if the "and x" evaluates to "false" (as it would if y is bigger than x) then "y" is returned.

This is equivalent to:

if x > y then
max = x
else
max = y
end
User avatar
Yggdrasil
Posts: 24
Joined: June 25th, 2011, 5:09 pm

Re: Help with Lua "and"

Post by Yggdrasil »

Yes, what I wrote is the same as jsmorley wrote with ? operator.
Int = (Length<=Width and Mode==2) and 0 or 1
Let's see:
(Length<=Width and Mode==2) <- this is your basic condition. It will be evaluated, and it will be a boolean: true, or false, depending on Length, Width, Mode. This is clear.
Now I assume it's true, and replace it in the above code:
true and 0 or 1
The and expression's return value is the left operand if that is nil or false, and the right operand if the left one has value. In our case, left operand in true, so the return value is the right operand, 0.
Now the or part:
0 or 1
or's value is the left operand, if it's not nil or not false, else the right operand. Now left operand is 0, so that's the result, and that's what you wanted.
If the expression is false:
false and 0 or 1
Return value is false, because false and 0 is false, see above.
false or 1
Return value is 1, see above :)
Hope it's clear.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Help with Lua "and"

Post by smurfier »

I get it! I get it! Yay!

Thank you!
Post Reply