gpt4 book ai didi

lua - 尝试调用 nil 值(字段 'maxn')错误

转载 作者:行者123 更新时间:2023-12-04 16:30:07 25 4
gpt4 key购买 nike

mytable = setmetatable({1,2,3}, {
__add = function(mytable, newtable)

for i = 1, table.maxn(newtable) do
table.insert(mytable, table.maxn(mytable)+1, newtable[i])
end
return mytable
end
})

secondtable = {4,5,6}

mytable = mytable + secondtable

for k,v in ipairs(mytable) do
print(k,v)
end

当我在终端中运行它时出现此错误:

lua: metatables4.lua:6: attempt to call a nil value (field 'maxn')
stack traceback:
metatables4.lua:6: in metamethod '__add'
metatables4.lua:15: in main chunk
[C]: in ?

但是当我尝试在 tutorialspoint 编译器上运行它时,它会运行。

1   1
2 2
3 3
4 4
5 5
6 6

这应该是我的输出。我无法确定这里到底是什么问题,因为它运行在教程点编码地面 lua 编译器上。

我应该改变什么让它在我的终端上工作?

最佳答案

来自 Lua 5.2 Reference Manual - 8.2 – Changes in the Libraries :

Function table.maxn is deprecated. Write it in Lua if you really need it.

您正在运行比 tutorialspoint 更新的 Lua 版本。

您可以在脚本顶部添加以下代码,使其在 Lua 5.1+ 版本中运行:

table.maxn = table.maxn or function(t) return #t end

关于lua - 尝试调用 nil 值(字段 'maxn')错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788068/

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