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

[lua] [Solved] Saving data

Started by darkrising, 17 October 2012 - 12:05 AM
darkrising #1
Posted 17 October 2012 - 02:05 AM
hi there, I was wonder if there is a better way of saving data than what I'm doing at the moment:


file = io.open("<filename>", "w")
file:write("Avar = "..var)
file:close()

At the moment I'm writing the varible directly into the file and running a dofile() to load it.

This method is ok for one or two varibles but soon the harddrive will fill up with more config files if there are too many varibles.

Is there a better way of saving config files?

For example only editing line 2 in the config file without effecting the other lines.


Var1 = true
Var2 = false -- Change this line only
Var3 = {1,2,3,4,5,6,7}

Or better yet just reading data instead of dofile()

true
false -- Change this line only
{1,2,3,4,5,6,7}

I will appreciate any help or advice you can give :D/>/>
Noodle #2
Posted 17 October 2012 - 02:08 AM
hi there, I was wonder if there is a better way of saving data than what I'm doing at the moment:


file = io.open("<filename>", "w")
file:write("Avar = "..var)
file:close()

At the moment I'm writing the varible directly into the file and running a dofile() to load it.

This method is ok for one or two varibles but soon the harddrive will fill up with more config files if there are too many varibles.

Is there a better way of saving config files?

For example only editing line 2 in the config file without effecting the other lines.


Var1 = true
Var2 = false -- Change this line only
Var3 = {1,2,3,4,5,6,7}

Or better yet just reading data instead of dofile()

true
false -- Change this line only
{1,2,3,4,5,6,7}

I will appreciate any help or advice you can give :D/>/>
Not really.. Writing to a file is the easiest way and recommended..
Orwell #3
Posted 17 October 2012 - 02:13 AM
I usually use 'textutils.serialize()' to serialize a table into text. Then I write that to a file, afterwards I load it and deserialize the string (textutils.deserialize()) to a table again. For example:

Config = { data={} }

function Config.load(filename)
  local h = fs.open(filename, 'r')
  local str = h.readAll()
  h.close()
  Config.data = textutils.unserialize(str)
end

function Config.save(filename)
  local h = fs.open(filename, 'w')
  local str = textutils.serialize(Config.data)
  h.write(str)
  h.close()
end

Config.load('config')
print(Config.data.testKey)
Noodle #4
Posted 17 October 2012 - 02:18 AM
I usually use 'textutils.serialize()' to serialize a table into text. Then I write that to a file, afterwards I load it and deserialize the string (textutils.deserialize()) to a table again. For example:

Config = { data={} }

function Config.load(filename)
  local h = fs.open(filename, 'r')
  local str = h.readAll()
  h.close()
  Config.data = textutils.deserialize(str)
end

function Config.save(filename)
  local h = fs.open(filename, 'w')
  local str = textutils.serialize(Config.data)
  h.write(str)
  h.close()
end

Config.load('config')
print(Config.data.testKey)
But still writing to a file..
Orwell #5
Posted 17 October 2012 - 02:23 AM
* snip *
But still writing to a file..

Eh, sure, I kinda didn't read your post yet. But I'm suggesting this to darkrising over 'dofile' : )
darkrising #6
Posted 17 October 2012 - 02:32 AM
the table method seems interesting I will look into it, but is there no way to write just to a specific line on a file?
ChunLing #7
Posted 17 October 2012 - 02:46 AM
Yes, there is an "apphend" option for fs.open http://computercraft.info/wiki/index.php?title=Fs.open However, writing/reading an entire table using textutils.un/serialize is quite powerful. I'd view the "a" option as mainly useful for some kind of log file.
Lyqyd #8
Posted 17 October 2012 - 03:33 AM
To write to a specific line in a file, it's usually easiest to read the whole file line-by-line into entries in a table. Then change the relevant entry in the table before writing it back to the file line by line.
ChunLing #9
Posted 17 October 2012 - 07:51 AM
Yes. You also gain the benefit of being able to use the information in the file right out of the table created when you unserialize the file. The main disadvantage is that the file isn't easily readable as text (being full of brackets and numbers and "="), so if you want a log file that you can easily peruse to see what your program has been doing, the apphend option provided by fs.open("filename","a") should fit your needs.

You can open and read an entire file as a single text string if you really need to, lua can easily accommodate book (or small library) sized text strings. You can then use string functions to manipulate the string, then save the entire string. But this isn't a highly efficient way to manipulate most data. The table functions of lua are a good bit more powerful than the string library.
darkrising #10
Posted 17 October 2012 - 03:32 PM
Okay, thanks for the info, I will try experiment with different methods see what I find easiest :D/>/>
Lyqyd #11
Posted 17 October 2012 - 03:53 PM
Yes. You also gain the benefit of being able to use the information in the file right out of the table created when you unserialize the file. The main disadvantage is that the file isn't easily readable as text (being full of brackets and numbers and "="), so if you want a log file that you can easily peruse to see what your program has been doing, the apphend option provided by fs.open("filename","a") should fit your needs.

You can open and read an entire file as a single text string if you really need to, lua can easily accommodate book (or small library) sized text strings. You can then use string functions to manipulate the string, then save the entire string. But this isn't a highly efficient way to manipulate most data. The table functions of lua are a good bit more powerful than the string library.

If this was in reply to my post, I would like to clarify that I was not suggesting using the serialization or unserialization functions, but rather creating a table containing the contents of each line of the file, one line per value.

Also, I think you underestimate the string library, or at least its usefulness in the context of ComputerCraft.
ChunLing #12
Posted 17 October 2012 - 06:15 PM
No, I find the string library to be enormously useful in making use of rednet for automated communication/control systems. But it still isn't anywhere near the power of tables in lua, as they are the native data structure.