с помощью функции type (), чтобы узнать, существует ли текущая строка в виде таблицы

Is it possible to see if a string is the same as the name of a table?

For example: I know that a table called 'os' exists, and I have a string "os". Is there then a way to do this:

x="os"
if type(x)=="table" then
    print("hurra, the string is a table")
end

Of course this example wont work like I want it to because

type(x)

will just return "string".

The reason why I want to do this, is just because I wanted to list all existing Lua tables, so I made this piece of code:

alphabetStart=97
alphabetEnd=122

function findAllTables(currString, length)

    if type(allTables)=="nil"   then
        allTables={}
    end

    if type(currString)=="table" then
        allTables[#allTables+1]=currString
    end

    if #currString < length then
        for i=alphabetStart, alphabetEnd do
            findAllTables(currString..string.char(i), length)
        end
    end
end

findAllTables("", 2)

for i in pairs(allTables) do
    print(i)
end

I wouldn't be surprised if there is an easier method to list all existing tables, I'm just doing this for fun in my progress of learning Lua.

11
задан RBerteig 10 March 2011 в 19:46
поделиться