Lua ternary operator - multiple variables -
say want assign 2 values 2 variables if condition true, , 2 different values if said condition false. assume done this:
a, b = 4 > 5 , 1, 2 or 3, 4
however assigns false, , b 2. if have:
a, b = 4 < 5 , 1, 2 or 3, 4
this correctly assigns 1 , b 2.
what missing here, how can "ternary operator" work expect?
you missing lua's and
, or
short-cutting , commas lower in hierarchy. happens here first 4 > 5 , 1
evaluated false
, 2 or 3
evaluated 2
, 4
ignored. in second case 4 < 5
true
, 4 < 5 , 1
1
, rest stays is.
as egor skriptunoff suggested can do
a, b = unpack(4 > 5 , {1,2} or {3,4})
instead.
Comments
Post a Comment