Posted 09 January 2016 - 04:22 AM
I seem to be having some problems in debugging my computercraft script. The script was created to allow the computer to communicate with an outside server that manages a database. It does so by making a call to an API which returns a response in a serialized lua table.
I seem to be getting an error saying it expected a table, but got nil. The website is on, returning the correct data (from what I can tell), and the domain api.bleauweb.net was added to the computercraft config.
startup:37: bad argument: table expected, got nil
I seem to be getting an error saying it expected a table, but got nil. The website is on, returning the correct data (from what I can tell), and the domain api.bleauweb.net was added to the computercraft config.
webStr = http.get('http://api.bleauweb.net/stargate?type=computercraft').readAll()
database = textutils.unserialize(webStr)
rednet.open('bottom')
stargate = peripheral.wrap('stargate_0')
function clear()
term.clear()
term.setCursorPos(1,1)
print("## Stargate Dialing Computer ##")
end
function update()
webStr = http.get('http://api.bleauweb.net/stargate?type=computercraft').readAll()
database = textutils.unserialize(webStr)
--print('Stargate Database Updated')
end
function userInput()
write('> ')
return read()
end
function parse(inputStr, sep)
if sep == nil then
sep = "%s"
end
local t={};i=1
for str in string.gmatch(inputStr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function search(alias, database)
update()
for i,v in ipairs(database) do
local aliases = parse(database[i].aliases, '|')
for c,d in ipairs(aliases) do
if (d == alias) then
return database[i].address
end
end
end
return false
end
clear()
while true do
local inputStr = userInput()
local parsed = parse(inputStr)
--print(inputStr)
--print(parsed[1])
--print(parsed[2])
if parsed[1] == 'dial' then
if search(parsed[2], database) ~= false then
stargate.connect(search(parsed[2], database))
else
stargate.connect(parsed[2])
end
elseif parsed[1] == 'clear' then
clear()
elseif parsed[1] == 'close' then
stargate.disconnect()
elseif parsed[1] == 'update' then
update()
end
end
startup:37: bad argument: table expected, got nil