如何将数据和表格插入现有表格?
How to insert data and tables into an existing one?
我无法将新行和表格插入到现有的行和表格中。
让我们调用来源 SourceFile.lua 及其简化的内容:
SourceFile = {};
SourceFile.list = {
BrandName1 = {
number = 10,
products = {
"Product1", 3,
"Product2", 4,
"Product3", 7,
},
},
BrandName2 = {
number = 5,
products = {
"Product1", 10,
"Product2", 3,
"Product3", 6,
},
},
-- and so on
}
我想做这样的事情:
require 'SourceFile'
local myData = {
BrandName2 = { -- add new products for the existing brand
products = {
"Product4", 2,
},
},
MyBrandName1 = { -- add new brand
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
},
-- and so on
}
table.insert(SourceFile.list, myData)
但是我的代码有问题,我得到了以下结果(用 inspect 打印):
{
list = { {
BrandName2 = {
products = { "Product4", 2 }
},
MyBrandName1 = {
number = 12,
products = { "MyProduct1", 21, "MyProduct2", 95 }
}
},
BrandName1 = {
number = 10,
products = { "Product1", 3, "Product2", 4, "Product3", 7 }
},
BrandName2 = {
number = 5,
products = { "Product1", 10, "Product2", 3, "Product3", 6 }
}
}
}
我做错了什么?
我是 lua 的新手,很确定这很明显,但对我来说不是。请帮帮我。
加法
在这些答案之后,我还找到了一个一个一个插入新品牌名称的方法:
SourceFile.list.MyBrandName1 = {
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
}
这并没有完全回答我的问题,但可能对 lua 的新手有用。
您正在将 table 个品牌名称推入品牌名称列表中。
这使它成为品牌名称列表 + table 和品牌名称。
table.insert(SourceFile.list, myData)
这会将 myData
插入到 SourceFile.list
。 myData
是带有品牌名称的 table。
SourceFile.list
也是带有品牌名称的 table。
列表中的列表。
你有 2 个选择来解决这个问题:
- 分别插入每个品牌名称
- 创建一个函数将
myData
的内容合并到 SourceFile.list
table.insert
将它的第二个参数添加到数组(它的第一个参数)。您的 SourceFile.list
应该只有字符串键,因此它不能用作数组。您需要一个递归函数来将数据从一个 table 合并到另一个:
local function meld(data, newData)
for k, v in pairs(newData) do
local oldValue = data[k]
if type(oldValue) ~= 'table' or type(v) ~= 'table' then
-- One of the values is not a table, so let's clobber the old value.
data[k] = v
else
-- Both are tables.
meld(oldValue, v)
end
end
end
meld(SourceFile.list, myData)
我无法将新行和表格插入到现有的行和表格中。
让我们调用来源 SourceFile.lua 及其简化的内容:
SourceFile = {};
SourceFile.list = {
BrandName1 = {
number = 10,
products = {
"Product1", 3,
"Product2", 4,
"Product3", 7,
},
},
BrandName2 = {
number = 5,
products = {
"Product1", 10,
"Product2", 3,
"Product3", 6,
},
},
-- and so on
}
我想做这样的事情:
require 'SourceFile'
local myData = {
BrandName2 = { -- add new products for the existing brand
products = {
"Product4", 2,
},
},
MyBrandName1 = { -- add new brand
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
},
-- and so on
}
table.insert(SourceFile.list, myData)
但是我的代码有问题,我得到了以下结果(用 inspect 打印):
{
list = { {
BrandName2 = {
products = { "Product4", 2 }
},
MyBrandName1 = {
number = 12,
products = { "MyProduct1", 21, "MyProduct2", 95 }
}
},
BrandName1 = {
number = 10,
products = { "Product1", 3, "Product2", 4, "Product3", 7 }
},
BrandName2 = {
number = 5,
products = { "Product1", 10, "Product2", 3, "Product3", 6 }
}
}
}
我做错了什么?
我是 lua 的新手,很确定这很明显,但对我来说不是。请帮帮我。
加法
在这些答案之后,我还找到了一个一个一个插入新品牌名称的方法:
SourceFile.list.MyBrandName1 = {
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
}
这并没有完全回答我的问题,但可能对 lua 的新手有用。
您正在将 table 个品牌名称推入品牌名称列表中。 这使它成为品牌名称列表 + table 和品牌名称。
table.insert(SourceFile.list, myData)
这会将 myData
插入到 SourceFile.list
。 myData
是带有品牌名称的 table。
SourceFile.list
也是带有品牌名称的 table。
列表中的列表。
你有 2 个选择来解决这个问题:
- 分别插入每个品牌名称
- 创建一个函数将
myData
的内容合并到SourceFile.list
table.insert
将它的第二个参数添加到数组(它的第一个参数)。您的 SourceFile.list
应该只有字符串键,因此它不能用作数组。您需要一个递归函数来将数据从一个 table 合并到另一个:
local function meld(data, newData)
for k, v in pairs(newData) do
local oldValue = data[k]
if type(oldValue) ~= 'table' or type(v) ~= 'table' then
-- One of the values is not a table, so let's clobber the old value.
data[k] = v
else
-- Both are tables.
meld(oldValue, v)
end
end
end
meld(SourceFile.list, myData)