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

Help With io.open

Started by elfin8er, 02 November 2012 - 04:32 AM
elfin8er #1
Posted 02 November 2012 - 05:32 AM
So I'm trying to write all of my variables to a file. I was able to get them to be written, but how can I set each line on a file to a different variable?
jag #2
Posted 02 November 2012 - 06:37 AM
If you want to make it simple write this

-- Your variables
var1 = 1
var2 = "two"
var3 = "III"

-- What to write to the file
file:write(textutils.serialize({var1,var2,var3}))

And to extract the variables from the file

var1, var2, var3 = textutils.unserialize(file:read())

See more about serializing here: serialize unserialize

But if you really want to save the variables on separate lines you can do

file:write(var1 .. "n" .. var2 .. "n" .. var3)
-- n = new line
KaoS #3
Posted 02 November 2012 - 07:12 AM
great advice there. you just forgot one thing, that way when you recall them var1 becomes a table of all values


var1, var2, var3 = unpack(textutils.unserialize(file:read()))

other than that it should work
elfin8er #4
Posted 02 November 2012 - 08:34 AM
I may just be doing something obvious, but I'm getting startup:5: attempt to call nil
elfin8er #5
Posted 02 November 2012 - 08:39 AM
I may have gotten it.
KaoS #6
Posted 02 November 2012 - 08:42 AM
could you post the code so we can check it?
elfin8er #7
Posted 02 November 2012 - 09:37 AM
I think I may just be misunderstanding this. Is there an in depth tutorial for this?
KaoS #8
Posted 02 November 2012 - 09:49 AM
allow me to make an inept attempt at explaining this:

a table is a list of values that can be accessed with their key.

Format:

tablename={} --declare a table
tablename.key=value
OR

tablename={}
tablename["key"]=value
OR

tablename{key=value}

if you do not specify a key in the third example then it gives them consecutive numeric values as keys:

tablename={'firstval','secondval','otherval','randomval'}
then tablename[1]=='firstval'

that is the basics of how tables work. tables cannot be sent over rednet or saved to a file so we use the serializer API to convert a table into a string

temp=textutils.serialize(tablename)
will make the variable 'temp' a string version of the table, you can write this to a file or send it over rednet. then you say

newtable=textutils.unserialize(temp)
to convert the string back into a table again where it can be used again. this is how tables and serialization works. all that is left is the unpack function


the unpack function goes through all consecutive values in a table (the keys must be consecutive numbers starting at 1) and returns them all so

temp={'valone','valtwo'}
return unpack(temp)
would return 2 things: 'valone' and 'valtwo'

you can use this to assign the values in a table to variables:

temp={'one','two'}
local a,b=unpack(temp)
then a='one' and b='two'

IN SUMMARY:

we are making your variables into a table, we then serialize that table and write it to a file, when we need the variables we read the string from the file and unserialize it to convert it back into a table, we then use the unpack function to split the values and assign them back to your values…

hope I made sense