Reading from, displaying, and parsing a text file

General discussion about LÖVE, Lua, game development, puns, and unicorns.
nunix
Prole
Posts: 28
Joined: Fri Jan 30, 2009 4:38 am

Reading from, displaying, and parsing a text file

Post 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.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

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

Post 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
nunix
Prole
Posts: 28
Joined: Fri Jan 30, 2009 4:38 am

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

Post 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).
Last edited by nunix on Fri Feb 06, 2009 3:22 am, edited 1 time in total.
User avatar
Kaze
Party member
Posts: 189
Joined: Sat Jul 19, 2008 4:39 pm
Location: Dublin, Ireland

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

Post 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.
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

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

Post by osgeld »

i knew someone had a better way hehe
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

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

Post 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)
})
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
nunix
Prole
Posts: 28
Joined: Fri Jan 30, 2009 4:38 am

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

Post by nunix »

Ah, so I'll have to go at it from a LUA angle.. okay, fair enough. Thanks for the assist.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

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

Post 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.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
User avatar
Skofo
Party member
Posts: 146
Joined: Mon Dec 22, 2008 10:55 pm

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

Post 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?
Working on: Viator
Need a 64-bit Debian package for LÖVE? Here it is!
User avatar
Sardtok
Party member
Posts: 108
Joined: Thu Feb 21, 2008 2:37 pm
Location: Norway/Norge/諾威/挪威 (Yes, I'm teh back!)
Contact:

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

Post 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. ;)
Take off every Zigg for great rapist.
Now, outgay that!
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests