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

Grabbing variable value from text file?

Started by dmillerw, 25 March 2012 - 08:57 AM
dmillerw #1
Posted 25 March 2012 - 10:57 AM
Idea is pretty simple. Say I have a text file, stored in the same place as the program being run. In the text file is just a list of variables, like so.


var1 = yes
var2 = no
var3 = yes

Is it possible to open said config file in the program, and pull out a certain value?
Espen #2
Posted 25 March 2012 - 11:03 AM
Yes you can, but, as far as I know, not out of the box.
That means you have to write a program which interprets the data in the file.
Or you can make use of the following serializer here: http://www.computercraft.info/forums2/index.php?/topic/302-api-proper-serialization
It stores strings, numbers, boolean values, etc. in files and is able to read them from them again. Very handy. :(/>/>
dmillerw #3
Posted 25 March 2012 - 11:11 AM
Not sure how that can be used for my purposes. Mind enlightening me?
Espen #4
Posted 25 March 2012 - 11:27 AM
Not sure how that can be used for my purposes. Mind enlightening me?
Basically his serialize() function encodes a variable into a string-form.
You can then save this string to a file for later use.
If you want to load the variable again, you just read the string from the file and call deserialize() on it.
The result will be your variable from the beginning.
And it doesn't matter what type your variable was (Well, mostly. See his thread for details). The type is preserved.

So basically you do…
serialize(yourVar)
… which will give you a gibberish looking string.
You store this wherever you want, and if the time comes you load this string up again and then deserialize it like this…
deserialize(gibberishString)
… which will return the variable you stored before.

EDIT: I assume here that you know how to read and write to files, as I thought the crux of your question was how to read variables from a file, not how to read from files in general. Was that a misinterpretation? :(/>/>
Edited on 25 March 2012 - 09:30 AM
dmillerw #5
Posted 25 March 2012 - 11:31 AM
Ah… alright. Makes sense.

One question left, and that's if it's possible to store multiple strings in one file? Or am I going to have multiple files, one for each configuration option?

I know how to read and write to files, yes. You interpreted it correctly. :(/>/>

EDIT: Have a slight problem… keep getting "lua:43: attempt to call table" whenever I try and use the serialize function.
Edited on 25 March 2012 - 09:41 AM
Espen #6
Posted 25 March 2012 - 11:39 AM
You can save multiple variables to file.
Either by storing one string per line and then loading it from there in the same fashion.

Or a much easier solution IMO:
  • Store all of the variables you want to save in a table first.
  • Serialize this table and store the result to file.

If you then want to load your variables again you do this:
  • Load the whole file
  • Deserialize it and pull all your variables from the resulting table

Hope it helps, cheers. :(/>/>