This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
akaFrancis's profile picture

"attempt to call table" while using linked list

Started by akaFrancis, 27 January 2013 - 05:15 AM
akaFrancis #1
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!

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!
Lyqyd #2
Posted 27 January 2013 - 07:48 AM
Split into new topic.
akaFrancis #3
Posted 27 January 2013 - 08:01 AM
Split into new topic.

Thanks a lot!
Lyqyd #4
Posted 27 January 2013 - 08:23 AM
The error is because you're trying to call a table. tempL.next is a table (the next table in the series), so tempL.next() attempts to call this table. Try removing the parentheses. Same with the tL line a little later.
Bubba #5
Posted 27 January 2013 - 08:29 AM
Additionally to Lyqyd's response, I noticed in your receiveCount() function that you perform a tostring(count) operation as opposed to a tonumber(count) operation which is what I imagine you meant to do. Could cause issues later :)/>
akaFrancis #6
Posted 27 January 2013 - 08:37 AM
The error is because you're trying to call a table. tempL.next is a table (the next table in the series), so tempL.next() attempts to call this table. Try removing the parentheses. Same with the tL line a little later.

Solved it thanks a lot.

Additionally to Lyqyd's response, I noticed in your receiveCount() function that you perform a tostring(count) operation as opposed to a tonumber(count) operation which is what I imagine you meant to do. Could cause issues later :)/>

I noticed that too, it's changed ingame, just not in the pastebin yet! Thanks anyways.
Lyqyd #7
Posted 27 January 2013 - 08:56 AM
Also, is there a reason for using a linked list here instead of another data structure? I'm having a hard time seeing the advantage of it in Lua.