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

Problem loading tables

Started by SNWLeader, 10 November 2014 - 11:22 PM
SNWLeader #1
Posted 11 November 2014 - 12:22 AM
I am trying to make a very nice os program for my computers and I am having a hell of a time trying to use tables to hold multiple peices of information.

I serialize the data into a new file and cannot get them to return.

I use this to serialize for a test.

function save(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end
local myTable = {}
term.clear()
term.setCursorPos(1,1)
print("What is your name?")
write("?: ")
input = read()
myTable["username"] = input
term.clear()
term.setCursorPos(1,1)
print("What is you password?")
write("?:  ")
input = read("*")
myTable["pwd"] = input
term.clear()
term.setCursorPos(1,1)
print("Type a color.")
write("?:  ")
input = read()
myTable["color"] = input
term.clear()
term.setCursorPos(1,1)
save(myTable,"info")
print("Table Made")

then I use this to unserialize test.

function load(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
textutils.unserialize(data)
file.close()
end
function resetLine(x,y)
term.setCursorPos(x,y)
term.clearLine()
term.setCursorPos(x,y)
end
function loadLogo()
local image = paintutils.loadImage("logo")
paintutils.drawImage(image, 3, 1)
end
function LoadingBar()
if pocket then
middle = 13 - 3
term.setCursorPos(10,19)
for i = 1, 4, 1 do
print("-oooO-")
sleep(0.5)
resetLine(10,19)
print("-ooOo-")
sleep(0.5)
resetLine(10,19)
print("-oOoo-")
sleep(0.5)
resetLine(10,19)
print("-Oooo-")
sleep(0.5)
resetLine(10,19)
print("-oOoo-")
sleep(0.5)
resetLine(10,19)
print("-ooOo-")
sleep(0.5)
resetLine(10,19)
end
else
middle = 26 - 5
for i = 1, 3 , 1 do
term.setCursorPos(21,18)
print("-ooooooooO-")
sleep(1)
resetLine(21,18)
print("-oooooooOo-")
sleep(1)
resetLine(21,18)
print("-ooooooOoo-")
sleep(1)
resetLine(21,18)
print("-oooooOooo-")
sleep(1)
resetLine(21,18)
print("-ooooOoooo-")
sleep(1)
resetLine(21,18)
print("-oooOooooo-")
sleep(1)
resetLine(21,18)
print("-ooOoooooo-")
sleep(1)
resetLine(21,18)
print("-oOooooooo-")
sleep(1)
resetLine(21,18)
print("-Ooooooooo-")
sleep(1)
resetLine(21,18)
end
end
term.setCursorPos(1,1)
end

term.clear()
term.setCursorPos(1,1)
load("info")
print(myTable[username])
print(myTable[pwd])
print(myTable[color])
Bomb Bloke #2
Posted 11 November 2014 - 01:18 AM
function load(name)
  local file = fs.open(name,"r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)  -- Send the unserialised data back.
end

.
.
.

local myTable = load("info")  -- Capture the returned data in "myTable".
print(myTable["username"])  -- Don't forget the quotes!
print(myTable["pwd"])
print(myTable["color"])
theoriginalbit #3
Posted 11 November 2014 - 01:34 AM
-snip-
in reality you may as well just use loadfile since textutils.unserialize is now just a glorified loadstring.


okay, ignore me, my memory failed and I was thinking loadfile did things differently than it did, it's close, but there's no "return " which is required
Edited on 11 November 2014 - 12:57 AM
SNWLeader #4
Posted 11 November 2014 - 04:45 PM
function load(name)
  local file = fs.open(name,"r")
  local data = file.readAll()
  file.close()
  return textutils.unserialize(data)  -- Send the unserialised data back.
end

.
.
.

local myTable = load("info")  -- Capture the returned data in "myTable".
print(myTable["username"])  -- Don't forget the quotes!
print(myTable["pwd"])
print(myTable["color"])

Thank you, I was having so much trouble and now it works. Thank you Bomb Cake. :D/>