Home > Enterprise >  creating variables using for loop in Lua
creating variables using for loop in Lua

Time:11-04

Im trying to create variables like these using a for loop..

TPidL1 = Load('TPidL1', '')
TPidL2 = Load('TPidL2', '')
TPidL3 = Load('TPidL3', '')
TPidL4 = Load('TPidL4', '')
TPidL5 = Load('TPidL5', '')

After reading other posts, I tried this but no luck

for z = 1, 5, 1 do  
    "TPidL"..z = Load('TPidL'..tostring(z), '')
end

Any ideas how I could approach this better?

thanks

CodePudding user response:

Just use a regular table instead of messing with globals..?

TPidLs = {}
for z = 1, 5, 1 do  
    TPidLs[z] = Load('TPidL' .. tostring(z), '')
end

CodePudding user response:

you can do this via the global namespace _G:

for z = 1, 5 do  
    _G["TPidL"..z] = "TPidL"..z
end
print(TPidL1,TPidL2,TPidL3,TPidL4,TPidL5)

CodePudding user response:

Well, AFAIK it's not possible directly, but Lua being quite flexible, there is a way. The key thing is to understand that all the global variables and functions are simply stored in a simple Lua table named _ENV. A simple example to highlight this:

MyVariable = "Hello"
for Key, Value in pairs(_ENV) do
  print(Key,Value)
end

This code will show something like this:

loadfile        function: 0000000065b9d5a0
os              table:    0000000000e19f20
collectgarbage  function: 0000000065b9d0a0
io              table:    0000000000e18630
error           function: 0000000065b9d020
_VERSION        Lua 5.4
_G              table:    0000000000e11890
print           function: 0000000065b9cd60
warn            function: 0000000065b9ccc0
arg             table:    0000000000e19ba0
table           table:    0000000000e18c50
type            function: 0000000065b9c780
next            function: 0000000065b9cea0
ipairs          function: 0000000065b9ce50
assert          function: 0000000065b9d6f0
debug           table:    0000000000e19f60
rawget          function: 0000000065b9cbc0
load            function: 0000000065b9d4a0
coroutine       table:    0000000000e18c90
pairs           function: 0000000065b9d380
string          table:    0000000000e19a20
select          function: 0000000065b9c890
tostring        function: 0000000065b9c860
math            table:    0000000000e19a60
setmetatable    function: 0000000065b9d2d0
MyVariable      1
dofile          function: 0000000065b9d670
utf8            table:    0000000000e19d20
rawequal        function: 0000000065b9cc70
pcall           function: 0000000065b9c7e0
tonumber        function: 0000000065b9c930
rawlen          function: 0000000065b9cc10
xpcall          function: 0000000065b9c6e0
require         function: 0000000000e177c0
package         table:    0000000000e17840
rawset          function: 0000000065b9cb60
getmetatable    function: 0000000065b9d610

That's it: _ENV is simple table which make the link between the function name and its implementation and between variable name and its value. Regarding you question, I have absolutely no idea what is the definition of the Load function.

But if you want to generate variable names dynamically, you could try:

for Index = 1, 5 do
  _ENV["MyVariable"..Index] = Index
end

print(MyVariable1)
print(MyVariable2)
print(MyVariable3)
print(MyVariable4)
print(MyVariable5)

It should print:

> print(MyVariable1)
1
> print(MyVariable2)
2
> print(MyVariable3)
3
> print(MyVariable4)
4
> print(MyVariable5)
5
>
  • Related