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

Table values not being detected?

Started by liquidobsidian, 27 June 2014 - 09:49 PM
liquidobsidian #1
Posted 27 June 2014 - 11:49 PM
Hey guys, for some reason x y and z values aren't being detected in my code. I probably did something stupid but I can't seem to figure it out.
And before you say it, I know the table exists and is being downloaded everytime.

All I want it to do is tell me which tower is the closest, so if you have an easier way of doing that than please tell me!



Error: sgps:17: attempt to perform arithmetic __add on nil and nil

http://pastebin.com/K1WyPcKc

--UPDATE SGPS
local towerdata = http.get("http://pastebin.com/raw.php?i=MCCNBq1a").readAll()
local fl = fs.open("data/twrs", "w")
fl.write(towerdata)
fl.close()
local h = fs.open("data/twrs", "r")
towers = h.readAll()
fl.close()
--END UPDATE SGPS
args = {...}
local towerstable = textutils.unserialize(towers)
local function getClosestTower()
  local twrst = {}
  local twrsn = {}
  for k,v in pairs(towerstable) do
	local t = v[x]+v[y]+v[z]
	table.insert(twrst, k)
	table.insert(twrsn, t)
  end
  local num = math.max(unpack(twrsn))
  local name = ""
  for k,v in pairs(items) do
	if(v==num) then
	  name = k
	end
  end
  return name
end
if args[1] == "locate" then
  local loc = gps.locate()
  --local loctext = loc[1]..", "..loc[2]..", "..loc[3]
  --print("You are at: "..loctext..".")
  print("Your closest tower is: "..getClosestTower())
end

http://pastebin.com/MCCNBq1a

{
["XiTech Tower 1"] = {x=-149,y=227,z=-24},
["Sertex #1"] = {x=74,y=224,z=-29},
["Sertex #2"] = {x=62,y=227,z=-111},
["Sertex #2"] = {x=62,y=227,z=-105},
["Sertex #3"] = {x=71,y=227,z=-26}
}
Dog #2
Posted 28 June 2014 - 12:11 AM
Error: sgps:17: attempt to perform arithmetic __add on nil and nil

http://pastebin.com/K1WyPcKc

--UPDATE SGPS
local towerdata = http.get("http://pastebin.com/raw.php?i=MCCNBq1a").readAll()
local fl = fs.open("data/twrs", "w")
fl.write(towerdata)
fl.close()
local h = fs.open("data/twrs", "r")
towers = h.readAll()
fl.close()

I'm not sure why you're getting nils on line 17 as I haven't reviewed the full code, but I did notice a couple of things right away that you may wish to look at (this won't solve your immediate problem)…

- Your second fl.close() should be h.close()

- Instead of reading the data into one variable, writing it out to a file, then reading the file back into another variable, would something like this work?

local towers = http.get("http://pastebin.com/raw.php?i=MCCNBq1a").readAll()
local fl = fs.open("data/twrs", "w")
fl.write(towers)
fl.close()
This avoids assigning two variables to hold the data and avoids the local file read step entirely.

However, if you're going to be reading the file in via http each time, there's probably no reason to write it to disk at all. You could simply have this…

local towers = http.get("http://pastebin.com/raw.php?i=MCCNBq1a").readAll()

EDIT: I've looked at your code and I'm still not seeing why you're getting nils. I'll take another look, but I think a more seasoned pro will need to help you with this. FWIW, the code you posted *should* be causing the error on line 16, not 17 (if I'm counting correctly).

EDIT2: Try this, it'll give you an error if the remote file doesn't exist (which will eliminate that possibility). In doing this, I noticed that you never close your http.get handle due to the way you read the data. The way you were calling readAll() *may* be why you were getting nils (I'm guessing here).

This is probably a better way to do it…


local URL = http.get("http://pastebin.com/raw.php?i=MCCNBq1a")
if URL then
  towers = URL.readAll()
  URL.close()
else
  term.clear()
  term.setCursorPos(1,1)
  term.write("Remote file does not exist")
  return
end

Hope that helps :)/>
Edited on 28 June 2014 - 12:11 AM
Bomb Bloke #3
Posted 28 June 2014 - 02:27 AM
IIRC, you can't just perform an http get and read all on the same line - you'll get nil back. It looks like it should work, but doesn't. Try splitting it up instead:

--UPDATE SGPS
local webHandle = http.get("http://pastebin.com/raw.php?i=MCCNBq1a")
local towerstable = webHandle.readAll()

local fl = fs.open("data/twrs", "w")
fl.write(towerstable)
fl.close()

--END UPDATE SGPS
args = {...}
local towerstable = textutils.unserialize(towerstable)

As Dog hints at, reading from a file you've just overwritten is completely pointless.

Re line 17, you'd be better off applying the Pythagorean theorem a couple of times, or your results will be inaccurate. (Edit: And don't forget to pass loc to the function so that it can actually do the comparison relative to where the turtle is!!)

Re line 25, you probably want twrst[k], not just k.
Edited on 28 June 2014 - 12:32 AM