lua:90: 尝试对字段“?”(值为空)进行索引。

当我调用它时,我的二维变量无法被正确识别。当我打印它时,它似乎可以正常工作,但是当我尝试从函数内部调用它时,它就变得毫无头绪了。

这是我的代码:

math.randomseed(os.time())
math.random(); math.random(); math.random()
-- init
local t = ""
-- t == type
local year = 2014
-- year is placeholder with no real value.
local i = 1
local x = 0
local y = 0
local z = 0
local o = 0
--
local l = 0
local l1 = 0
local l2 = 0
--
local h = 1
-- Junk Variables
local month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}
local days = {0, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
-- Days linked to months, weeks come as calculated
local m = 1
-- "m" is the real month value to be used within the code as the literal value.
fd = {}          -- create the matrix
    for y=1,5 do
      fd[y] = {}     -- create a new row
      for z=1,5 do
        fd[y][z] = 0
      end
    end
-- fd = Family/Day
na = {1, 2, 3, 4 ,5}
-- numbers not allocated for the day
fv = {}
--[12][days[m]][5]
-- final value to be printed literally into string of txt file.
local s = ""
-- string variable
io.write("Please enter a month (ONLY USE NUMBERS)")
io.flush()
m=io.read()
io.write("Please enter a chore creation type (daily, weekly, monthly [Case sensitive])")
t=io.read()
--
m = tonumber(m)
--
for y=1,12 do
    fv[y] = {}
    for z=1,days[m] do
        fv[y][z] = {}
        for o=1,5 do
            fv[y][z][o] = 0
        end
    end
end
--
if t == "daily" then
    local f,err = io.open("ChoreDaily.txt","w")
    if not f then return print(err) end
    f:write("")
    --
    repeat
        i = 0
        y = 0
        print(">>")
        repeat
            if h <= days[m] then
            --
                repeat
                    if h <= days[m] then
                        --
                        os.execute("cls")
                        l1 = math.random(1,2)
                        l2 = math.random(3,4)
                        l = math.random(l1,l2)
                        repeat
                            o = math.random(1,5)
                            l = l-1
                        until l == 0
                        --
                        if y == 0 then
                            --
                            if na[o] > 0 then
                                if x < 4 then
                                    s = s .. tostring(na[o]) .. ", "
                                elseif x >= 4 then
                                    s = s .. tostring(na[o])
                                end
                                fd[x][y] = na[o] -- this is the problem.
                                na[o] = 0
                                x = x+1
                                print("!")
                            end
    --

我认为我的总体目标相当明显,但这是一个家务清单的创建器。非常基本,我希望我可以全部自己完成,但是不幸的是,如果我无法利用二维变量,我就无法有更多的进展了。

有一些未使用的变量和其他东西挂在那里。我打算稍后把它们清除。

点赞
用户1009479
用户1009479

x的初始值为0,在尝试访问fd[x][y]之前不会更改。但是,表格fd的有效索引为15,这意味着在这里fd[x]nil,您无法访问fd[x][y]

2014-03-03 13:45:45