Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表函数中的Lua集合表元素_Lua_Scope_Lua Table - Fatal编程技术网

表函数中的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
函数的结果是all
nil
。为什么
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