无法操作 Lua 表
Trouble manipulating Lua tables
假设我有两个 tables:
veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}
veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}
我想以一个 table 结束,它代表:
veggie_multiples = {
{veggie = "carrot", quantity = 3},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 4},
{veggie = "potato", quantity = 2}}
我试过类似的方法:
veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}
veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}
for i, n in ipairs(veggie_singles) do
for ii, nn in ipairs(veggie_multiples) do
if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
table.remove(veggie_singles, i)
else
table.insert(veggie_multiples, {veggie = veggie_singles[i], quantity = 1})
table.remove(veggie_singles, i)
end
end
end
for i, n in ipairs(veggie_multiples) do
print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end
无论我怎样尝试,它都无法正常工作。请帮忙!谢谢。
这个有效:
for i, n in ipairs(veggie_singles) do
local existed = false
for ii, nn in ipairs(veggie_multiples) do
if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
existed = true
break
end
end
if not existed then
table.insert(veggie_multiples, {veggie = veggie_singles[i].veggie, quantity = 1})
end
end
仅当 veggie_singles[i].veggie
中的项目不等于 veggie_multiples
中的 all 项时,才会插入新项目。这就是标志 existed
的作用。
使用 ipairs 的 for 循环遍历索引和值
for i, n in ipairs(veggie_singles)
将在第一次迭代中给出 i=1 and n={veggie="celery"}
,依此类推。代码不需要使用 i
所以在 Lua 中你使用 _ 作为一个扔掉。然后在 veggie multiples 中搜索与 veggie single 同名的条目。找不到就添加,找到就增加数量。
for _, vs in pairs(veggie_singles) do
local found = false
for _, vm in pairs(veggie_multiples) do
if vm.veggie == vs.veggie then
vm.quantity = vm.quantity + 1
found = true
break
end
end
if not found then
table.insert(veggie_multiples, {veggie=vs.veggie, quantity=1})
end
end
for i, n in ipairs(veggie_multiples) do
print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end
由于索引对解决问题不是很有用,而名称是您要查找内容的依据,因此您可以使用名称作为表中的键来简化代码。
quantity = {carrot=1, tomato=2, celery=3}
add = {"celery", "carrot", "potato", "carrot", "potato"}
for _, v in pairs(add) do
quantity[v] = (quantity[v] or 0) + 1
end
for veggie, qty in pairs(quantity) do
print(veggie, qty)
end
输出:
potato 2
carrot 3
celery 4
tomato 2
假设我有两个 tables:
veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}
veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}
我想以一个 table 结束,它代表:
veggie_multiples = {
{veggie = "carrot", quantity = 3},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 4},
{veggie = "potato", quantity = 2}}
我试过类似的方法:
veggie_multiples = {
{veggie = "carrot", quantity = 1},
{veggie = "tomato", quantity = 2},
{veggie = "celery", quantity = 3}}
veggie_singles = {
{veggie = "celery"},
{veggie = "carrot"},
{veggie = "potato"},
{veggie = "carrot"},
{veggie = "potato"}}
for i, n in ipairs(veggie_singles) do
for ii, nn in ipairs(veggie_multiples) do
if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
table.remove(veggie_singles, i)
else
table.insert(veggie_multiples, {veggie = veggie_singles[i], quantity = 1})
table.remove(veggie_singles, i)
end
end
end
for i, n in ipairs(veggie_multiples) do
print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end
无论我怎样尝试,它都无法正常工作。请帮忙!谢谢。
这个有效:
for i, n in ipairs(veggie_singles) do
local existed = false
for ii, nn in ipairs(veggie_multiples) do
if veggie_singles[i].veggie == veggie_multiples[ii].veggie then
veggie_multiples[ii].quantity = veggie_multiples[ii].quantity + 1
existed = true
break
end
end
if not existed then
table.insert(veggie_multiples, {veggie = veggie_singles[i].veggie, quantity = 1})
end
end
仅当 veggie_singles[i].veggie
中的项目不等于 veggie_multiples
中的 all 项时,才会插入新项目。这就是标志 existed
的作用。
使用 ipairs 的 for 循环遍历索引和值
for i, n in ipairs(veggie_singles)
将在第一次迭代中给出 i=1 and n={veggie="celery"}
,依此类推。代码不需要使用 i
所以在 Lua 中你使用 _ 作为一个扔掉。然后在 veggie multiples 中搜索与 veggie single 同名的条目。找不到就添加,找到就增加数量。
for _, vs in pairs(veggie_singles) do
local found = false
for _, vm in pairs(veggie_multiples) do
if vm.veggie == vs.veggie then
vm.quantity = vm.quantity + 1
found = true
break
end
end
if not found then
table.insert(veggie_multiples, {veggie=vs.veggie, quantity=1})
end
end
for i, n in ipairs(veggie_multiples) do
print(veggie_multiples[i].veggie, " ", veggie_multiples[i].quantity)
end
由于索引对解决问题不是很有用,而名称是您要查找内容的依据,因此您可以使用名称作为表中的键来简化代码。
quantity = {carrot=1, tomato=2, celery=3}
add = {"celery", "carrot", "potato", "carrot", "potato"}
for _, v in pairs(add) do
quantity[v] = (quantity[v] or 0) + 1
end
for veggie, qty in pairs(quantity) do
print(veggie, qty)
end
输出:
potato 2
carrot 3
celery 4
tomato 2