Reading a text file

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
mdjenkins86
Posts: 8
Joined: Fri Nov 11, 2005 12:49 am
Location: Philly

Reading a text file

Post by mdjenkins86 »

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.
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

If you add a code snippet for us to understand better, we'll help you. Ok?
Geo Massar
Retired Engineer
Bob535
Posts: 56
Joined: Fri Nov 04, 2005 6:52 am

Post by Bob535 »

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.
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

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 }] )

Code: Select all

1,2,3,4,5
then you could easily create an array out of it

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

Post by Durante »

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.
mdjenkins86
Posts: 8
Joined: Fri Nov 11, 2005 12:49 am
Location: Philly

Post by mdjenkins86 »

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.
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

Post by Durante »

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
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Post by JoshDB »

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
Contents of the file NAME (text file with no .txt extension):
default username
To write to the file NAME, just use:

Code: Select all

writeVar("NAME",username)
mdjenkins86
Posts: 8
Joined: Fri Nov 11, 2005 12:49 am
Location: Philly

Post by mdjenkins86 »

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...
Post Reply