【发布时间】:2012-07-20 07:27:57
【问题描述】:
正如标题所说,我可以通过什么函数或检查来确定 lua 元素是否为表格?
local elem = {['1'] = test, ['2'] = testtwo}
if (elem is table?) // <== should return true
【问题讨论】:
正如标题所说,我可以通过什么函数或检查来确定 lua 元素是否为表格?
local elem = {['1'] = test, ['2'] = testtwo}
if (elem is table?) // <== should return true
【问题讨论】:
print(type(elem)) -->table
Lua 中的 type 函数返回它的第一个参数是什么数据类型(字符串)
【讨论】:
在原始问题的上下文中,
local elem = {['1'] = test, ['2'] = testtwo}
if (type(elem) == "table") then
-- do stuff
else
-- do other stuff instead
end
希望这会有所帮助。
【讨论】:
您可能会发现这有助于提高可读性:
local function istable(t) return type(t) == 'table' end
【讨论】:
使用type():
local elem = {1,2,3}
print(type(elem) == "table")
-- true
【讨论】: