• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

How to deal with nil values in variables and concatenation them

Explorer ,
Feb 03, 2024 Feb 03, 2024

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.

 

TOPICS
SDK

Views

176

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 03, 2024 Feb 03, 2024

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

...

Votes

Translate

Translate
Community Expert ,
Feb 03, 2024 Feb 03, 2024

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. 

 

Sean McCormack. Author of 'Essential Development 3'. Magazine Writer. Former Official Fuji X-Photographer.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

.. StrOption1 or "" ..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 03, 2024 Feb 03, 2024

Copy link to clipboard

Copied

LATEST

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines