表函数中的Lua集合表元素
以下是片段:表函数中的Lua集合表元素,lua,scope,lua-table,Lua,Scope,Lua Table,以下是片段: local t = {} t.tt = {} function t.xx() for i=1,10 do t.tt[i] = i end end for i=1,10 do print(t.tt[i]) end print函数的结果是allnil。为什么t.tt中的所有元素都是nil 在打印之前,您需要实际运行该函数: local t = {} t.t
local t = {}
t.tt = {}
function t.xx()
for i=1,10 do
t.tt[i] = i
end
end
for i=1,10 do
print(t.tt[i])
end
print
函数的结果是allnil
。为什么t.tt
中的所有元素都是nil 在打印之前,您需要实际运行该函数:
local t = {}
t.tt = {}
function t.xx()
for i=1,10 do
t.tt[i] = i
end
end
-- execute function here
t.xx()
for i=1,10 do
print(t.tt[i])
end
或者只分配值:
local t = {}
t.tt = {}
-- no function here
for i=1,10 do
t.tt[i] = i
end
for i=1,10 do
print(t.tt[i])
end