2014-03-03 89 views
2

我无法让我的二维变量在我称之为正确识别时。当我打印它时,它似乎工作正常,但是当我尝试从一个函数内调用它时,它会在我身上播放香蕉。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 
    -- 

我认为这是相当明显发生了什么,我试图做的整体,但它是一个苦差事列表制作。很原始,我希望自己可以做到这一切,但不幸的是,如果我不能利用2维变量,我将无法做得更多。

有一些未使用的变量和什么不挂。我打算以后摆脱那些。

回答

2

x初始化为0,并且在尝试访问fd[x][y]之前未更改。但表fd的有效索引是从15,这意味着fd[x]nil在这里,您不能访问fd[x][y]

+0

是的。这是它(我认为,仍然测试,因为我的程序挂起)。 – user3375027

相关问题