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

Help on loading and saving a table?

Started by Tunnerrocks, 23 April 2014 - 12:55 AM
Tunnerrocks #1
Posted 23 April 2014 - 02:55 AM
So first off I've been writing a "server" script so when it gets a certain message it'll write to a file located on the server, and then read that file for client names, and client balance. My problem is I can write to the file, but it keeps overwriting my previous work, so then i tried table.insert, which in turn made my table nil! Please someone just help me with this one, usually I can find the bugs in my own script, but this one I've been fighting with for too long. here's the script:

modem = peripheral.wrap("right")
rednet.open("right")
originalnames = {}
function save (table, clients) -- a save function
local file = fs.open("config/clients", "w")
file.write(textutils.serialize(originalnames))
file.close()
end
if fs.exists("config") ~= true -- create configure folder for clients and credit
  then fs.makeDir("config")
print("Config directory created")
sleep(1)
end

if fs.exists("config/clients") ~= true -- writes {} to config/clients (happens only on first run)
then save(table, originalnames)
print("client file created")
sleep(1)
term.clear()
term.setCursorPos(1,1)
end
function save(table, names)  -- a save function
local file = fs.open("config/clients", "w")
file.write(textutils.serialize(table))
file.close()
end
function load(table, names) -- loads clients table into environment
local file = fs.open("config/clients","r")
local data = file.readAll()
file.close()
return textutils.serialize(data)
end

load(table,names) -- actually loads the table
write("input mode: ")
mode = read()
if mode == "new client"
then
write("account number: ")
  accn = read("*")
  if accn == "1"
   then write("name: ")
   txt = read()
   table.insert(table, 1, txt)
   save(names)

  elseif accn == "2"
   then write("name: ")
   txt = read()
   table.insert(table, 2, txt)
   save(names)
(the rest of the script is from 3-16, plus a whole different segment, this portion is what I've been testing with, and where the error originates)
Bomb Bloke #2
Posted 23 April 2014 - 06:56 AM
"table" is a pre-made table which contains functions for working with tables. There's no need to pass it to your save/load functions, as they already have access to it. You certainly do NOT want to insert things into it.

When a function returns something, you'll generally want to capture that information somewhere. For example, "read()" returns what was typed; you hence use "txt = read()" to capture what was typed in the "txt" variable. When you call your "load()" function, however, you don't capture the returned table anywhere, so that data gets lost.

textutils.serialize() and textutils.unserialise() are not interchangeable.

Generally, there should never be a need to define two different versions of a given function under the same name. While the Lua VM won't complain, it makes it a lot harder for people to read your code for no benefit.

Here's an attempt at cleaning things up a bit:

Spoiler
modem = peripheral.wrap("right")
rednet.open("right")

local clients = {}

-- Define functions we'll be using throughout the script:

local function save()
	local file = fs.open("config/clients", "w")
	file.write(textutils.serialize(clients))
	file.close()
end

local function load() -- loads clients table into environment
	local file = fs.open("config/clients","r")
	local data = file.readAll()
	file.close()
	return textutils.unserialize(data)
end

-- Check our data folder + file exist:

if not fs.exists("config") then
	fs.makeDir("config")
	print("Config directory created")
	sleep(1)
end

if not fs.exists("config/clients") then -- writes {} to config/clients (happens only on first run)
	save()
	print("client file created")
	sleep(1)
	term.clear()
	term.setCursorPos(1,1)
end

-- Get on with the script proper:

clients = load() -- actually loads the table

write("input mode: ")
local mode = read()

if mode == "new client" then
	write("account number: ")
	local accn = tonumber(read("*"))
	
	write("name: ")
	local txt = read()
		
	clients[accn] = txt
	save()
end

It could still use a lot of work, mind you, but I don't want to change things to the point where you can't see the parallels between this and what you've been trying to do. By all means ask about any bits that don't make sense to you.