Reputation: 2126
I'm trying to recieve data from a pretty big table (bigTable
) and the getRow
function should rearrange some data for faster calculation (specific information below). The problem is that some of this values don't exist (-->are nil
). I thought I'd taken care of this by adding the if-Statement
to check first if the value does even exist, but I still get the error below. Thanks for your help.
My function (from line 46):
function getRow(a, b)
row = {}
for d = 0, 3 do
if (bigTable[a + d][b + d]) then
table.insert(row, bigTable[a + d][b + d])
end
end
return row
end
Error:
C:\Program Files (x86)\Lua\5.1\lua.exe: .\solution_11.lua:49: attempt to index field '?' (a nil value)
stack traceback:
.\solution_11.lua:49: in function 'getRow'
.\solution_11.lua:69: in function 'diagonal'
.\solution_11.lua:89: in main chunk
[C]: ?
The
getRow()
-function should get the values of the two dimensional array from point A, B "diagonally" downwards.
Upvotes: 1
Views: 282
Reputation: 2215
The goal can be achieved by exchanging this line
if (bigTable[a + d][b + d]) then
with this line
if a and b and type(bigTable[a + d])=='table' and bigTable[a + d][b + d] then
This solves the problem because every possibility is checked (a
or b
can't be nil
, the table you trying to access is even existent and it contains the value you trying to access). You only checked for the last one and therefore you got an error when the value was nil
.
Upvotes: 1