Page 1 of 1

Reading a file

Posted: Tue Aug 14, 2012 9:56 am
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!

Re: Reading a file

Posted: Tue Aug 14, 2012 10:35 am
by Nixola
Try

Code: Select all

if line ~= "\n" or line ~= "a\n" then

Re: Reading a file

Posted: Tue Aug 14, 2012 10:41 am
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.

Re: Reading a file

Posted: Tue Aug 14, 2012 10:44 am
by Nixola
...Right. Thanks, even if I'm not the OP

Re: Reading a file

Posted: Tue Aug 14, 2012 10:59 am
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...

Re: Reading a file

Posted: Tue Aug 14, 2012 11:35 am
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?

Re: Reading a file

Posted: Tue Aug 14, 2012 12:51 pm
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

Re: Reading a file

Posted: Tue Aug 14, 2012 4:37 pm
by Roland_Yonaba
Well, it is not possible to do such a thing in Lua, by default.
But, you may use Meta...