Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string inside variable reference

Tags:

lua

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

like image 221
Ron Avatar asked Jul 02 '26 10:07

Ron


1 Answers

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

like image 117
Vlad Avatar answered Jul 05 '26 17:07

Vlad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!