【发布时间】:2014-06-21 07:25:30
【问题描述】:
在 Lua 中,如何在如下表中插入新列?
table t = {
{name = "John", age = 19, sex = "M"},
{name = "Susan", age = 20, sex = "F"},
{name = "Paul", age = 18, sex = "M"}
}
我想在名称前放一列id,所以表格可能是这样的:
table t = {
{id = 1, name = "John", age = 19, sex = "M"},
{id = 2, name = "Susan", age = 20, sex = "F"},
{id = 3, name = "Paul", age = 18, sex = "M"}
}
PS:此表的数据来自如下文件:
entry {name = "John", age = 19, sex = "M"}
entry {name = "Susan", age = 20, sex = "F"}
entry {name = "Paul", age = 18, sex = "M"}
我正在使用此代码将此数据插入到表中:
data = {}
text = file:read()
do
function entry(entrydata)
table.insert(data, entrydata)
end
thunk = load(text, nil, nil, {entry = entry})
thunk()
end
【问题讨论】: