Reading a file

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
onper
Prole
Posts: 3
Joined: Tue Aug 14, 2012 9:39 am

Reading a file

Post by onper »

Hi!

I have this small problem when reading the lines of a text document using the following code.

Code: Select all

for line in love.filesystem.lines("questions.txt") do
		if line ~= "" or line ~= "a" then
			table.insert(canvas_questions, line)
		end
		
		print(line) -- for debugging purposes
	end
The text file, as an example

Code: Select all

Raggadish

Dunadump
a
Ropperish
The problem is that the code adds the line to the table even if it contains an empty space or an a. I have a feeling that this could be caused by the "new line" unicode, but I haven't made any progress there, which I'm asking you to help me.

I know I can probably solve this by checking the line with a string function to see it's first character matches a etc.

Thank you for the help!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Reading a file

Post by Nixola »

Try

Code: Select all

if line ~= "\n" or line ~= "a\n" then
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Reading a file

Post by Robin »

No.

Replace or by and.

If you do "a ~= b or a ~= c" (with b ~= c) then whatever the case, it always returns true. Think about it.
Help us help you: attach a .love.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Reading a file

Post by Nixola »

...Right. Thanks, even if I'm not the OP
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
onper
Prole
Posts: 3
Joined: Tue Aug 14, 2012 9:39 am

Re: Reading a file

Post by onper »

Thanks for getting back with solutions so quickly!

It worked perfectly fine with replacing or with and, now I kind of feel stupid about that mistake...
onper
Prole
Posts: 3
Joined: Tue Aug 14, 2012 9:39 am

Re: Reading a file

Post by onper »

Another question while I'm at it :awesome:

I'm a little experienced with Java and when you're increasing an int, you can type

Code: Select all

i++
While in lua you have to do

Code: Select all

i = i +1
is there some way to do it in lua like the java example?
User avatar
Petunien
Party member
Posts: 191
Joined: Fri Feb 03, 2012 8:02 pm
Location: South Tyrol (Italy)

Re: Reading a file

Post by Petunien »

As far as I know there's no possibility to increment a number like in java or elsewhere.

You have to do:

Code: Select all

i = i +1
"Docendo discimus" - Lucius Annaeus Seneca
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Reading a file

Post by Roland_Yonaba »

Well, it is not possible to do such a thing in Lua, by default.
But, you may use Meta...
Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests