Copy link to clipboard
Copied
I have some checkboxes that represents options for executing an external program.
some pseudo code:
if box == checked
strOption1 = aa
if box 2 == checked
strOption2 == bb
strCommand = "software " .. strOption1 .. " " .. strOption2
------
But if the strOption(s) are empty (nil) I get an error.
I could the write some if else but that would make it more complicated.
Building on kimaldis' reply, in your example you'll need parentheses around the "or" expression:
strCommand = "software " .. (strOption1 or "") .. " " .. strOption2
This will evaluate to "" if "strOption1" is nil or false, to "strOption1" otherwise.
More generally, the common way to approximate a conditional expression in Lua (which lacks them) is to write:
cond and x or y
which usually is "y" if "cond" is nil or false, "x" otherwise. But you need to be very careful: if "x" is nil or false, then t
...Copy link to clipboard
Copied
Would initialising the strings prevent this? Obviously you'd need an option for that intialising, but it could be the default choice.
Copy link to clipboard
Copied
.. StrOption1 or "" ..
Copy link to clipboard
Copied
Building on kimaldis' reply, in your example you'll need parentheses around the "or" expression:
strCommand = "software " .. (strOption1 or "") .. " " .. strOption2
This will evaluate to "" if "strOption1" is nil or false, to "strOption1" otherwise.
More generally, the common way to approximate a conditional expression in Lua (which lacks them) is to write:
cond and x or y
which usually is "y" if "cond" is nil or false, "x" otherwise. But you need to be very careful: if "x" is nil or false, then the value of the expression is always "y" (a different result than conditional expressions in other languages).