Dynamic variables

Because the global table is just a table, it can be indexed dynamically. You can concatenate strings to index the global table using the square bracket notation. For example, the following three lines of code all do the same thing:

foo = "bar"
_G.foo = "bar"
_G["foo"] = "bar"

In the preceding code example, the last line indexes the global environment with a string. The following code demonstrates how to index the global table using dynamic strings:

value1 = 'x'
value2 = 'y'
value3 = 'z'

for i = 1,3 do
print ("value" .. i .. ": " .. _G["value" .. i])
end
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.27.72