For loop bug; prints all values with each index

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
redsled
Prole
Posts: 38
Joined: Wed Jun 04, 2014 6:33 am

For loop bug; prints all values with each index

Post 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:
Attachments
parse.love
Killing me^^
(552 Bytes) Downloaded 113 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

Post 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
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 0 guests