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

Using Tables Read from another File

Started by cdel, 14 October 2014 - 08:36 AM
cdel #1
Posted 14 October 2014 - 10:36 AM
I was working on the script for my Operating Enviroment, I have encountered some problems. When using the table I read from a file, it's not working as I want. I am trying to use the information in the table to label the computer, please help me figure it out. (I'm not this terrible, im tired. :3)

Code

local maind = "/Luna/"
local version = "1.0"
os.version = function()
  return "Luna "..version
end
if not term or not shell or not fs then
  error("Luna: Not Running in CraftOS.", 0)
  return
end
if not shell.getRunningProgram() == "/Luna/init" then
  error("Luna: Not Running as '/Luna/init'.")
  return
end
local sysd = {
  prgs = maind.."Prg/",
  apis = maind.."Api/",
  syst = maind.."Sys/",
}
for _, v in pairs(sysd) do
  if not fs.exists(v) then
    error("Luna: Missing Directories, Run '/Luna/Sys/Update'", 0) 
    return
  end
end
local apis = fs.list(maind.."Api")
for _, v in pairs(apis) do
  os.loadAPI(sysd.apis..v)
  print("Loaded API: "..v)
end
local prgs = fs.list(sysd.prgs)
print(#prgs.. " Programs Detected.")
if not fs.exists(maind.."Dat") then
  fs.makeDir(maind.."Dat")
end
local data = { }
if not fs.exists(maind.."Dat/System") then
  write("Computer Name: ")
  local compname = read()
  data.hostname = compname
  local file = fs.open(maind.."Dat/System", "w")
  file.write(textutils.serialise(data))
  file.close()
end
local file = fs.open(maind.."Dat/System", "r")
local system = textutils.unserialise(file.readAll())
file.close()
system = textutils.serialise(system)
os.setComputerLabel(system.hostname)
wieselkatze #2
Posted 14 October 2014 - 02:24 PM
So you wrote

local system = textutils.unserialise(file.readAll())
to bring your string back to a table.
Why

system = textutils.serialise(system)
?

This will just convert your table back to a serialized string.
Delete that line and it should work perfectly fine.

Also, the next time, please post your error message. If it "worked as you want", you wouldn't be in the Ask a Pro section ;)/>
Edited on 14 October 2014 - 12:27 PM
cdel #3
Posted 15 October 2014 - 06:35 AM
Thanks B)/>