forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading a text file

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
mdjenkins86



Joined: 11 Nov 2005
Posts: 8
Location: Philly

PostPosted: Fri Nov 11, 2005 12:54 am    Post subject: Reading a text file Reply with quote

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
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Fri Nov 11, 2005 3:03 am    Post subject: Reply with quote

If you add a code snippet for us to understand better, we'll help you. Ok?
_________________
Geo Massar
Retired Engineer
Back to top
View user's profile Send private message Send e-mail
Bob535



Joined: 04 Nov 2005
Posts: 56

PostPosted: Fri Nov 11, 2005 5:06 am    Post subject: Reply with quote

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
View user's profile Send private message
LuMo



Joined: 21 Aug 2005
Posts: 410
Location: Austria

PostPosted: Fri Nov 11, 2005 5:26 am    Post subject: Reply with quote

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:
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
Back to top
View user's profile Send private message Visit poster's website
Durante



Joined: 02 Oct 2005
Posts: 65
Location: Austria

PostPosted: Fri Nov 11, 2005 8:59 am    Post subject: Reply with quote

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
View user's profile Send private message
mdjenkins86



Joined: 11 Nov 2005
Posts: 8
Location: Philly

PostPosted: Fri Nov 11, 2005 9:51 am    Post subject: Reply with quote

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
View user's profile Send private message
Durante



Joined: 02 Oct 2005
Posts: 65
Location: Austria

PostPosted: Fri Nov 11, 2005 11:04 am    Post subject: Reply with quote

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
View user's profile Send private message
JoshDB



Joined: 05 Oct 2005
Posts: 87

PostPosted: Fri Nov 11, 2005 11:05 am    Post subject: Reply with quote

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):

Quote:
default username


To write to the file NAME, just use:

Code:
writeVar("NAME",username)
Back to top
View user's profile Send private message Send e-mail AIM Address
mdjenkins86



Joined: 11 Nov 2005
Posts: 8
Location: Philly

PostPosted: Fri Nov 11, 2005 11:35 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group