x
is initialized as 0
, and is not changed before you try to access fd[x][y]
. But the valid index of the table fd
is from 1
to 5
, which means fd[x]
is nil
here, you can't access fd[x][y]
.
lua:90: attempt to index field '?' (a nil value)
-
19-10-2022 - |
Question
I cannot get my 2 dimensional variable to be recognized correctly when I call it. When I print it, it seems to work fine, but when I attempt to call it from within a function it goes bananas on me.
Here is my code:
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
--
I think it's pretty obvious what I am attempting to make overall, but it's a chore list creator. pretty primitive and I was hoping I could do it all myself, but unfortunately if I can't utilize 2 dimensional variables I'm not going to be able to go much further.
There are some unused variables and whatnot hanging around. I plan to get rid of those later.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow