Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一张桌子:
Table = { button = {}, window = {}, label = {}, edit = {}, error = {} }
如何获取表的键和值?
我试图得到:
for key, value in ipairs(Table) do for k, v in ipairs(key) do print(k, v) end end
但它不起作用。
ipairs用于序列(即,类似数组的表)。但是Table在你的代码中不是一个序列,你需要使用pairs来代替。
ipairs
Table
pairs
另一个问题是在 中Table,键是字符串("button"等"window")。那是因为在表构造函数中,button = {}等价于["button"] = {}.
"button"
"window"
button = {}
["button"] = {}
您需要迭代(现在为空)表的值。
for key, value in pairs(Table) do for k, v in pairs(value) do print(k, v) end end