Search code examples
variablesluaenvironment-variablesquotes

LUA After adding names from _ENV into a table, how do I print the VALUE from the name and not just the "name"


    X=123
    Y=321
    TAB1={}
    z=1
    for n in pairs(_ENV) do
        TAB1[z] = n
        z=z+1
    end

-- did some more steps that removes general _ENV and only left with my script global variables

    TAB2={x, y} 
    print(TAB2[1]) 

what I want is "123" but I'm getting "x"

I've tried gmatch, gsub, tostring, but I still can't get it to print the value of x (123). it's only printing "x"

**sorry I don't know how to work this text box correctly for my question


Solution

  • Ive tried gmatch, gsub, tostring, but I still can't get it to print the value of x (123). it's only printing "x"

    These functions are for string manipulation so I don't see how they'd be useful here. The reason for the lack of "value" is that your loop is for n in pairs(_ENV) do. pairs is just a wrapper for next which returns key and value, but you're currently discarding the value. Simply add a second variable to the for name list: for name, value in pairs(_ENV) do print(value) end. Alternatively, index _ENV using n: print(_ENV[n]) (inside the loop).