Lua string inside variable reference

Is there a way to "concatenate" variable references with strings ?:

fat_greek_wedding = 0;
nationality = "greek";

"fat_" .. nationality .. "_wedding" = 1; -- fat_greek_wedding == 1

      

Or maybe something like:

fat_greek_wedding = 0;
nationality = "greek";

fat_(nationality)_wedding = 1; -- fat_greek_wedding == 1

      

FYI I am writing code for Unified Remote that uses Lua: https://github.com/unifiedremote/Docs

+3


source to share


2 answers


Global variables or fields of structures are just elements of some table, and the variable name is a text key in this table.

If this fat_greek_wedding

is a global variable, you can access it like this:



fat_greek_wedding = 0;
nationality = "greek";

_G["fat_" .. nationality .. "_wedding"] = 1;

      

Here you are explicitly accessing the global environment by modifying / creating an element by the name that was created at runtime. In fact, this is the same thing that worksfat_greek_wedding=1

+7


source


Try the following:



loadstring("fat_"..nationality.."_wedding = 1")()

      

-2


source







All Articles