如何在 table 中从最后一个索引迭代到第一个
How to iterate from last index to first in table
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
我有这个 table,如何从最后一个索引迭代到第一个?它应该打破依赖于 lvl
。我只想从最好的武器中展示这个table。例如,如果玩家的等级为 53,那么我只想显示他 lvl
或更低等级的武器。我需要从最好的(顶部)展示它为什么我想从最后一个索引开始迭代。有人可以帮忙吗?
编辑:
感谢帮助。还有一个问题,我需要稍后更改 table。它显示一切正常,但我需要稍后从这个(更改的)列表中购买所有物品。所以我必须以某种方式替换这 2 tables。有没有简单的方法可以做到这一点?我试图从这个 table 中删除元素,但它仍然不起作用。
或者可以在Lua中制作一些地图?它必须是动态大小的,所以我猜我不能使用 table。有键值的东西
一个数字 for 循环,倒数,是最好的选择:
local t = {2,4,6,8}
for i = #t, 1, -1 do
print(t[i])
end
假设 table 不一定按级别排序(与示例不同),我们需要做两件事:
- 找出哪些剑在等级范围内
- 按降序排列
现在临时table中的第一个是"best"剑。
像这样:
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
myLevel = 53 -- wanted level
-- temporary table
possible = { }
-- extract ones which are in range
for k, v in ipairs (bestSword) do
if v.lvl <= myLevel then
table.insert (possible, v)
end -- if
end -- for
if #possible == 0 then
print "No matching swords"
else
table.sort (possible, function (a, b) return a.atk > b.atk end )
bestSword = possible [1]
print ("Best sword is", bestSword.name, "lvl =", bestSword.lvl,
"atk = ", bestSword.atk)
end -- if
Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value
Lua中的表格是 地图。每个 table 有 key/value 对。您在那里使用的是简单的数字键 tables.
所有 table 都是动态调整大小的。
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
我有这个 table,如何从最后一个索引迭代到第一个?它应该打破依赖于 lvl
。我只想从最好的武器中展示这个table。例如,如果玩家的等级为 53,那么我只想显示他 lvl
或更低等级的武器。我需要从最好的(顶部)展示它为什么我想从最后一个索引开始迭代。有人可以帮忙吗?
编辑: 感谢帮助。还有一个问题,我需要稍后更改 table。它显示一切正常,但我需要稍后从这个(更改的)列表中购买所有物品。所以我必须以某种方式替换这 2 tables。有没有简单的方法可以做到这一点?我试图从这个 table 中删除元素,但它仍然不起作用。
或者可以在Lua中制作一些地图?它必须是动态大小的,所以我猜我不能使用 table。有键值的东西
一个数字 for 循环,倒数,是最好的选择:
local t = {2,4,6,8}
for i = #t, 1, -1 do
print(t[i])
end
假设 table 不一定按级别排序(与示例不同),我们需要做两件事:
- 找出哪些剑在等级范围内
- 按降序排列
现在临时table中的第一个是"best"剑。
像这样:
bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
{name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
{name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}
myLevel = 53 -- wanted level
-- temporary table
possible = { }
-- extract ones which are in range
for k, v in ipairs (bestSword) do
if v.lvl <= myLevel then
table.insert (possible, v)
end -- if
end -- for
if #possible == 0 then
print "No matching swords"
else
table.sort (possible, function (a, b) return a.atk > b.atk end )
bestSword = possible [1]
print ("Best sword is", bestSword.name, "lvl =", bestSword.lvl,
"atk = ", bestSword.atk)
end -- if
Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value
Lua中的表格是 地图。每个 table 有 key/value 对。您在那里使用的是简单的数字键 tables.
所有 table 都是动态调整大小的。