当我调用二维变量时,我无法正确识别它。当我打印它时,它似乎工作正常,但是当我尝试从函数内调用它时,它让我大吃一惊。
这是我的代码:
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
--
我认为我总体上尝试做的事情非常明显,但它是一个家务列表创建器。非常原始,我希望我可以自己完成这一切,但不幸的是,如果我不能利用二维变量,我将无法走得更远。
有一些未使用的变量和诸如此类的东西。我计划稍后摆脱这些。
最佳答案
x
初始化为 0
,并且在尝试访问 fd[x][y]
之前不会更改。但是表fd
的有效索引是从1
到5
,这意味着fd[x]
是nil
此处,您无法访问 fd[x][y]
。
关于multidimensional-array - lua:90: 尝试索引字段 '?' (零值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148038/