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

[Lua][Error] Attempt to index ? (a nil value)

Started by Henness, 16 December 2012 - 01:30 PM
Henness #1
Posted 16 December 2012 - 02:30 PM
I'm making a updater for my programs so people don't have to download the new versions and programs.

But I cant pull my variables from my config file. I get Attempt to index ? (a nil value) on line 67, and Attempt to index ? (a nil value) on line 46 when the apconfig exists.


-- Advanced Updater by Henness
-- Version 1.0 12/15/2012
-- Config
local author = "Henness0666"
local project = "Advanced-Programs"
local branch = "master"
-- Functions
function loadTable(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function saveTable(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end
function getLink(file)
return "https://raw.github.com/" .. author .. "/" .. project .. "/" .. branch .. "/" .. file
end
function download(file, name)
local data = http.get(getLink(file))
if data then
  local file = fs.open(name,"w")
  file.write(data.readAll())
  file.close()
  return true
end
end
function updatePrograms()
term.clear()
term.setCursorPos(1,1)
if fs.exists("apconfig") then
  if download("config", "tmpconfig") then
   config = loadTable("apconfig")
   tmpconfig = loadTable("tmpconfig")
   for i=1,tmpconfig.programs do
	if tmpconfig[i].version > config[i].version and i <= config.programs then
	 file = config[i].name
	 tempfile = tmpconfig[i].name
	 temppath = tmpconfig[i].path
	 if fs.exists(file) then
	  fs.delete(file)
	 end
	 download(tempfile, temppath)
	elseif tmpconfig.programs > config.programs and i > config.programs then
	 download(tempfile, temppath)
	end
   end
   fs.delete("apconfig")
   saveTable(tmpconfig, "apconfig")
   fs.delete("tmpconfig")
  else
   print("Unable to download config file form GitHub.")
  end
else
  if download("config", "apconfig") then
   config = loadTable("apconfig")
   for i=1,config.programs do
	file = config[i].name
	path = config[i].path
	if fs.exists(file) then
	 fs.delete(file)
	end
	download(file, path)
	print("Program" .. file .. "updated")
   end
  else
   print("Unable to download config file form GitHub.")
  end
end
end
-- RUN
updatePrograms()
if fs.exists("orefindergui")then
shell.run("orefindergui")
end

This is my config file that the gets downloaded.

{["advancedgui"]={["name"]="advancedgui",["path"]="advancedgui",["version"]=1,},["advancedtunnel"]={["name"]="advancedtunnel",["path"]="advancedtunnel",["version"]=1,},["advancedorefinder"]={["name"]="advancedguiorefinder",["path"]="advancedorefinder",["version"]=1,},["programs"]=3,}
theoriginalbit #2
Posted 16 December 2012 - 02:39 PM
you are creating the file as a dictionary. then trying to reference by index. that may be the issues. maybe change the for i = 1 to a for-each loop. :)/>
Henness #3
Posted 16 December 2012 - 02:49 PM
you are creating the file as a dictionary. then trying to reference by index. that may be the issues. maybe change the for i = 1 to a for-each loop. :)/>

How would I do that, I have never done a for each loop. Couldn't I just change the ["advancedgui"] to [1] and so on?
theoriginalbit #4
Posted 16 December 2012 - 02:53 PM
you could change it to 1, 2, 3 etc. or else a for each is done like this


for key, value in pairs(tableName) do
  print(key.." is the bit on the left of the =")
  print(value.." is the bit on the right of the =")
end
Henness #5
Posted 16 December 2012 - 02:54 PM
Thanks
theoriginalbit #6
Posted 16 December 2012 - 02:55 PM
your welcome.
Henness #7
Posted 16 December 2012 - 05:55 PM

  if download("config", "apconfig") then
   config = loadTable("apconfig")
   for key,value in pairs(config) do
    if fs.exists(key) then
	 fs.delete(key)
    end
    download(key, config[key].path)
    print("Program" .. key .. "added.")
   end
  else
   print("Unable to download config file form GitHub.")
  end

How do I get the value of config[key].path??? because config[key].path doesnt work.
theoriginalbit #8
Posted 16 December 2012 - 06:00 PM
config["path"]

is that what you wanted?
Henness #9
Posted 16 December 2012 - 06:11 PM
No I want config["advancedgui"].path
["advancedgui"]={["path"]="advancedgui",["version"]=1,}
theoriginalbit #10
Posted 16 December 2012 - 06:17 PM
ooohhh. dictionary in a dictionary.


config[key]["path"]