好吧,我知道如何使用它们的初始值创建表/元表,但我不知道如何在创建后插入或删除元素。如何使用Lua Script中的最佳实践来完成此操作?这样做有什么标准功能吗?
答案 0 :(得分:3)
这里几乎是从Lua表中插入和删除的每种方式;首先,对于数组样式表:
local t = { 1, 2, 3 }
-- add an item at the end of the table
table.insert(t, "four")
t[#t+1] = 5 -- this is faster
-- insert an item at position two, moving subsequent entries up
table.insert(t, 2, "one and a half")
-- replace the item at position two
t[2] = "two"
-- remove the item at position two, moving subsequent entries down
table.remove(t, 2)
对于哈希样式表:
local t = { a = 1, b = 2, c = 3 }
-- add an item to the table
t["d"] = 4
t.e = 5
-- remove an item from the table
t.e = nil