gpt4 book ai didi

multidimensional-array - 创建 Lua 高级表

转载 作者:行者123 更新时间:2023-12-02 07:10:20 25 4
gpt4 key购买 nike

需要创建一些表,以便我可以通过这种方式从中获取信息:

table[attacker][id]

如果我会用

print(table[attacker][id])

它应该打印

试了很多方法,还没找到好的...

我猜应该是这样的……

table.insert(table, attacker, [id] = value)

^ 这行不通。

有人可以帮助我吗?


编辑

好吧,当我这样尝试时:

x = {}
function xxx()
if not x[attacker][cid] then
x[attacker][cid] = value
else
x[attacker][cid] = x[attacker][cid] + value
end
print(x[attacker][cid])
end

我收到一条错误消息:

attempt to index field '?' (a nil value)

最佳答案

您需要大括号来创建内表:

table.insert(my_table, attacker, {[id]=value})

-- the advantage of this is that it works even if 'attacker' isn't a number
my_table[attacker] = {[id]=value}

a = 1
b = 2
c = 3
d = {}
table.insert(d, a, {[b]=c})
print(d[a][b]) -- prints '3'

关于multidimensional-array - 创建 Lua 高级表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com