Page 1 of 2

Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 1:52 am
by nunix
Yay newbie questions.

Irritation: I actually tried searching the forum for this, but a phrase like "text file" was rejected because the words are too common. -.-

Issue:

I'm trying to read from a configuration file. I have no idea how to do this. I've done a bunch of googlin' but this seems to be too simple for anyone to bother with tutorial examples. =p So I'm looking to do the following:

Here's a sample text file:

Code: Select all

name:Ogg
type:caveman
stats:123
Here's what I want the screen to look like when I load the game:

Code: Select all

This is the <type> <name> whose Muscles is <first digit of stats:> and Brains is <second digit of stats:> and Feets is <third digit of stats:>
Help? If this is in the documentation or wiki somewhere you can just point me that direction, but I've had no luck finding this stuff on my own.

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 2:02 am
by osgeld
what your looking for is part of core lua, try ...

http://www.lua.org/manual/5.1/

or more to the point

http://www.lua.org/manual/5.1/manual.ht ... tring.find

and

http://www.lua.org/manual/5.1/manual.ht ... string.sub

personally in lua (and the fact i HATE string fidgeting) i usually just put everything in a lua file and include it

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 2:48 am
by nunix
Yeah, I need more than that. I'm staring at the Strings and Patterns sections of the manual and cannot find what I'm looking for. I can get a set word pattern, but how do I get, say, the third character? And how do I feed that back so I can do something with it?

EDIT: Ah, string.sub is (one of) the things that I wanted, I had to dig up further to get into the whole index-idea (the only significant coding I've done is on MUSH and MUX, which is a scripting language but also has some specialised functions and operators for this kind of thing).

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 3:20 am
by Kaze

Code: Select all

for line in love.filesystem.lines("data.txt") do
	for name, data in line:gmatch("(.-):(.*)") do
		print(name, data)
	end
end
Like that.

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 3:51 am
by osgeld
i knew someone had a better way hehe

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 8:30 am
by osuf oboys
nunix wrote:Yay newbie questions.

Irritation: I actually tried searching the forum for this, but a phrase like "text file" was rejected because the words are too common. -.-

Issue:

I'm trying to read from a configuration file. I have no idea how to do this. I've done a bunch of googlin' but this seems to be too simple for anyone to bother with tutorial examples. =p So I'm looking to do the following:

Here's a sample text file:

Code: Select all

name:Ogg
type:caveman
stats:123
Here's what I want the screen to look like when I load the game:

Code: Select all

This is the <type> <name> whose Muscles is <first digit of stats:> and Brains is <second digit of stats:> and Feets is <third digit of stats:>
Help? If this is in the documentation or wiki somewhere you can just point me that direction, but I've had no luck finding this stuff on my own.
The LUA way of doing these things however, is to make the file interpretable and load it upon start. For instance, to create a units folder, a unit.add function, and a code that iterates through and loads everything in the units folder (use love.filesystem.enumerate("units/")). Ogg would could like the following.

Code: Select all

unit.add({
  name="Ogg",
  type="Caveman",
  stats=123,
  craveforpie=math.random(10,14)
})

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 10:25 pm
by nunix
Ah, so I'll have to go at it from a LUA angle.. okay, fair enough. Thanks for the assist.

Re: Reading from, displaying, and parsing a text file

Posted: Fri Feb 06, 2009 11:04 pm
by osuf oboys
nunix wrote:Ah, so I'll have to go at it from a LUA angle.. okay, fair enough. Thanks for the assist.
No, Kaze showed how to read files of the exemplified format. An option would be to do it the LUA way.

Re: Reading from, displaying, and parsing a text file

Posted: Sat Feb 07, 2009 12:08 am
by Skofo
osuf oboys wrote:
nunix wrote:Yay newbie questions.

Irritation: I actually tried searching the forum for this, but a phrase like "text file" was rejected because the words are too common. -.-

Issue:

I'm trying to read from a configuration file. I have no idea how to do this. I've done a bunch of googlin' but this seems to be too simple for anyone to bother with tutorial examples. =p So I'm looking to do the following:

Here's a sample text file:

Code: Select all

name:Ogg
type:caveman
stats:123
Here's what I want the screen to look like when I load the game:

Code: Select all

This is the <type> <name> whose Muscles is <first digit of stats:> and Brains is <second digit of stats:> and Feets is <third digit of stats:>
Help? If this is in the documentation or wiki somewhere you can just point me that direction, but I've had no luck finding this stuff on my own.
The LUA way of doing these things however, is to make the file interpretable and load it upon start. For instance, to create a units folder, a unit.add function, and a code that iterates through and loads everything in the units folder (use love.filesystem.enumerate("units/")). Ogg would could like the following.

Code: Select all

unit.add({
  name="Ogg",
  type="Caveman",
  stats=123,
  craveforpie=math.random(10,14)
})
Would this method also work if you want to write to the file? For a save file, perhaps?

Re: Reading from, displaying, and parsing a text file

Posted: Wed Feb 11, 2009 5:35 pm
by Sardtok
I wouldn't recommend making save files easily readable.
There might be some kind of serialization in Lua, which might be less readable, but I can't say for sure.
How to make save games depend a bit on the game, but I would recommend, if possible, to have a simple fixed format: X bytes of this data, Y bytes of that data, Z bytes of some other data. Like a C struct.
Unless all of the data are strings, parts will be unreadable. That is, if you read and write it in binary and not as characters that are parsed. Watched out for endianness, though. I think Macs are Big Endian, at least the older PowerPC ones are.
Windows and most Linuxes(if not all) are Little Endian, so you might need to make sure things are written in a certain byte order.
If all this seems confusing, and you trust your users, go ahead with a human readable format. ;)