using type() function to see if current string exist as table

interjay gave the best way to actually do it. If you're interested, though, info on your original question can be found in the lua manual. Basically, you want:

mystr = "os"

f = loadstring("return " .. mystr);

print(type(f()))

loadstring creates a function containing the code in the string. Running f() executes that function, which in this case just returns whatever was in the string mystr.


If you want to iterate over all global variables, you can use a for loop to iterate over the special _G table which stores them:

for key, value in pairs(_G) do
    print(key, value)
end

key will hold the variable name. You can use type(value) to check if the variable is a table.

To answer your original question, you can get a global variable by name with _G[varName]. So type(_G["os"]) will give "table".

Tags:

Lua