| View previous topic :: View next topic |
| Author |
Message |
mdjenkins86
Joined: 11 Nov 2005 Posts: 8 Location: Philly
|
Posted: Fri Nov 11, 2005 12:54 am Post subject: 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. |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Fri Nov 11, 2005 3:03 am Post subject: |
|
|
If you add a code snippet for us to understand better, we'll help you. Ok? _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
Bob535
Joined: 04 Nov 2005 Posts: 56
|
Posted: Fri Nov 11, 2005 5:06 am Post subject: |
|
|
does it need to come from a text file?
you can always just store the array in a .lua file and then call that file. |
|
| Back to top |
|
 |
LuMo
Joined: 21 Aug 2005 Posts: 410 Location: Austria
|
Posted: Fri Nov 11, 2005 5:26 am Post subject: |
|
|
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 _________________ "Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com |
|
| Back to top |
|
 |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Fri Nov 11, 2005 8:59 am Post subject: |
|
|
if you want to store a table the best way is to just make a file bla.txt (or .data or anything)
mytable = { 2, 5, 9, "bla" }
and load with
dofile("bla.txt")
Look at the highscore saving / loading in Crystalise for an example. |
|
| Back to top |
|
 |
mdjenkins86
Joined: 11 Nov 2005 Posts: 8 Location: Philly
|
Posted: Fri Nov 11, 2005 9:51 am Post subject: |
|
|
Okay I will give it a try.
Basically I am making an rpg and want to make it possible to store data in the form of a table into an external file for saving reasons...txt or whatever. This is an awesome idea. I will post my results Friday when I get back home. Anymore ideas are welcome. |
|
| Back to top |
|
 |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Fri Nov 11, 2005 11:04 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
JoshDB

Joined: 05 Oct 2005 Posts: 87
|
Posted: Fri Nov 11, 2005 11:05 am Post subject: |
|
|
| Code: | 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 |
Contents of the file NAME (text file with no .txt extension):
To write to the file NAME, just use:
| Code: | | writeVar("NAME",username) |
|
|
| Back to top |
|
 |
mdjenkins86
Joined: 11 Nov 2005 Posts: 8 Location: Philly
|
Posted: Fri Nov 11, 2005 11:35 am Post subject: |
|
|
thanks to all. I really appreciate this. Of course, credit will be given where credit its due...Thanks
This and that silly loadstring thing were really giving me the run around. The problem is that I am used to programming C and the same things are done in different ways... |
|
| Back to top |
|
 |
|