Posted 27 January 2013 - 06:15 AM
While trying to use a linked list in my program, I get an "attempt to call table" exception that breaks the program. I have no Idea why that is the case so I'm asking for help here. I'm quite sure it's not because we've reached the end of the list, as the while loop should prevent us from accessing an out-of-bounds index/list node.
Anyways here's the code (a pastebin link: http://pastebin.com/nbpVh9bD for line numbers)
The Error happens on Line 33 at tempL = tempL.next()
Any help would be much appreciated!
Anyways here's the code (a pastebin link: http://pastebin.com/nbpVh9bD for line numbers)
The Error happens on Line 33 at tempL = tempL.next()
Any help would be much appreciated!
local Location = {}
function Location.new(x,y,z)
local o = {}
o.x = x
o.y = y
o.z = z
setmetatable(o, {__index = Location})
return o
end
function sendCatalogueReq()
rednet.open("right")
rednet.broadcast("count")
rednet.close("right")
receiveCount()
end
function receiveCount()
rednet.open("left")
rednet.broadcast("")
s, count = rednet.receive()
count = tostring(count)
rednet.broadcast("")
i = 0
while i < count do
s, id = rednet.receive()
rednet.broadcast("")
si, amount = rednet.receive()
local tempL = list
while tempL do
if tempL.itemID == id then
tempL.amount = tempL.amount + amount
break
end
tempL = tempL.next()
end
i = i + 1
rednet.broadcast("added")
end
rednet.close("left")
end
function serveLocReqs()
rednet.open("left")
while rednet.receive() ~= "finished" do
itemID = rednet.receive()
local tL = list
while tL do
if tL.itemID == itemID then
rednet.broadcast(tL.loc.x)
rednet.broadcast(tL.loc.y)
rednet.broadcast(tL.loc.z)
break
end
tL = tL.next()
end
end
end
function printContents()
tL = list
while tL do
print(tL.key .. tostring(amount))
tL = tL.next()
end
end
local ingot = Location.new(-679, 113, 295)
function addList()
list = { next = list, amount = 0, key = "refined iron", loc = ingot, itemID = 30249}
list = { next = list, amount = 0, key = "silver", loc = ingot, itemID = 4349}
list = { next = list, amount = 0, key = "iron", loc = ingot, itemID = 265}
list = { next = list, amount = 0, key = "tin", loc = ingot, itemID = 30247}
list = { next = list, amount = 0, key = "gold", loc = ingot, itemID = 266}
list = { next = list, amount = 0, key = "steel", loc = ingot, itemID = 7788}
list = { next = list, amount = 0, key = "copper", loc = ingot, itemID = 30248}
list = { next = list, amount = 0, key = "bronze", loc = ingot, itemID = 5261}
end
list = nil
addToList()
sendCatalogueReq()
printContents()
edit: fixed title!