Reading a text file
Moderators: Shine, Insert_witty_name
-
- Posts: 8
- Joined: Fri Nov 11, 2005 12:49 am
- Location: Philly
Reading a text file
Does any know how to read an array (table) from a text file into memory...
For example my table is stored in my.txt as :
[{1,2,3,4,5}]
whey I try to read it in to lua it becomes "[{1,2,3,4,5}]"
is there and way to get to this table without the quotes.
For example my table is stored in my.txt as :
[{1,2,3,4,5}]
whey I try to read it in to lua it becomes "[{1,2,3,4,5}]"
is there and way to get to this table without the quotes.
i have some code on my page, that
explodes an string to array
so if you store your data like this:
(maybe just cut off the [{ and }] )
then you could easily create an array out of it
greets
lumo
explodes an string to array
so if you store your data like this:
(maybe just cut off the [{ and }] )
Code: Select all
1,2,3,4,5
greets
lumo
-
- Posts: 8
- Joined: Fri Nov 11, 2005 12:49 am
- Location: Philly
There is a function in the crystalise library that serializes an arbitrary table without circular references to a stream, which makes saving of tabular data completely trivial. (loading is already trivial, as you can just dofile() the save)
The function name is serialize and it's near the bottom of src/misc.lua
The function name is serialize and it's near the bottom of src/misc.lua
Code: Select all
function writeVar(file, var)
file = io.open(file, "w")
if file then
file:write(var)
file:close()
end
end
function loadVar(file)
file = io.open(file, "r")
if file then
result = file:read("*l")
file:close()
end
end
loadVar("NAME")
username = result
To write to the file NAME, just use:default username
Code: Select all
writeVar("NAME",username)
-
- Posts: 8
- Joined: Fri Nov 11, 2005 12:49 am
- Location: Philly