Reputation: 175
Is there any way to "concatenate" variable references with strings?:
fat_greek_wedding = 0;
nationality = "greek";
"fat_" .. nationality .. "_wedding" = 1; -- fat_greek_wedding == 1
Or perhaps something like:
fat_greek_wedding = 0;
nationality = "greek";
fat_(nationality)_wedding = 1; -- fat_greek_wedding == 1
FYI I am writing code for Unified Remote, which uses Lua: https://github.com/unifiedremote/Docs
Upvotes: 3
Views: 2321
Reputation: 5867
Global variables, or fields of structures - are just elements of some table, and variable's name is a text key in that table.
If that 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 explicitly access global environment, altering/creating element by the name that was constructed in run time. Effectively it is the same as running fat_greek_wedding=1
Upvotes: 7