Page 1 of 1

For loop bug; prints all values with each index

Posted: Thu May 21, 2015 5:31 am
by redsled
Not good at making subject titles, but here's the problem: I'm trying to pass a .txt file and parse it. The only thing though is that I want to print it to the screen. All goes well and dandy, but for some reason (probably obvious) it prints the first line of the file, then it proceeds to the first and second... straight across the string.

Example output:

Code: Select all

1 0 0 0
1 0 0 0 1 0 0 0 2
1 0 0 0 1 0 0 0 2 1 0 0 0
Of this .txt file:

Code: Select all

1,0,0,0,
1,0,0,0,2,
1,0,0,0,
I feel like I'm missing some simple knowledge of lua/logic, but I can't find a answer to it. Here's the for loop:

Code: Select all

y = 0
x = 0
for i=1,#num do
	local v = num[i]
	love.graphics.print(v, x, y)
	
	x = x + 15
	if v == "" then
		y = y + 15
		x = 0
	end
end
Thanks a bunch,
- redsled :megagrin:

Re: For loop bug; prints all values with each index

Posted: Thu May 21, 2015 12:30 pm
by Robin
Try this:

Code: Select all

local function parseTextFile(filename)

	if love.filesystem.exists(filename) then
		for line in love.filesystem.lines(filename) do
			for s in (line .. ","):gmatch("([^,]*),") do
				table.insert(num, s)
			end
		end
	else
		return false
	end
end
The problem was that you kept adding all the lines to this string when parsing, and used that to add new stuff to the table