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

Another set of eyes. Need help debugging program.

Started by ErdMutter92, 09 January 2016 - 03:22 AM
ErdMutter92 #1
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.


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
Lyqyd #2
Posted 09 January 2016 - 05:22 AM
Let's play, "What's Wrong With This String?":


title = 'Slime King's Island',
Dog #3
Posted 09 January 2016 - 06:07 AM
I'm curious about this line…

local t={};i=1

In the above case, will 'i' be local as well? My guess is no, but I've only seen the following format for assigning multiple locals on a single line in Lua…

local t, i = { }, 1
Lyqyd #4
Posted 09 January 2016 - 06:20 AM
No, the i variable would not be local, the semicolon makes it a completely separate statement.
Bomb Bloke #5
Posted 09 January 2016 - 06:58 AM
… and a space, or even no dividing character at all, would do the same thing. If you suddenly throw in a symbol that can only be part of a new statement, then the Lua interpreter will spot that fact without the benefit of line breaks or whatever.