Page 1 of 1

[solved, stupid programmer me]<eof> error - string.gmatch()

Posted: Fri Mar 30, 2012 7:31 am
by timmeh42
Since I couldn't find a lua function equivalent to java's string.split(), which splits a string around a given string or character (actually regular expression which seems to be about equal to lua's patterns) and returns a table of the values, I decided to try making my own. Looking around the Lua reference manual, I found a function called string.gmatch() that seemed to suit my needs, and I wrote a little function to split a string into only alphanumeric strings (according to the pattern I gave).

Code: Select all

[b]function splitstring(string)
	local return_table
	for val in string.gmatch(string,"%w+") do
		table.insert(return_table,val)
	end
	return return_table
end[/b]

love.load()
	stringtable = splitstring("stat;354;332;0.6;0;stalactite_01")
end

love.draw()
	for n=0,#stringtable,1 do
		love.graphics.print(stringtable[n],32,32+n*32,0,1,1)
	end
end
(That's actually the whole love file)

The problem is, when I run it I get an error

Code: Select all

Syntax error: main.lua:11: '<eof>' expected near 'end'

Re: <eof> error - using string.gmatch()

Posted: Fri Mar 30, 2012 8:59 am
by trubblegum
It's probably not a good idea to override the global string lib with local string. It may not actually be causing problems, but it is generally considered bad practice.

Also :

Code: Select all

str:gmatch('%w+')
Unfortunately, you're not even getting that far, because what should be function definitions are actually calls, which is making the end at lines 11 and 17 irrelevant, and making the interpreter sad.

Re: <eof> error - using string.gmatch()

Posted: Fri Mar 30, 2012 9:12 am
by Wojak

Code: Select all

function splitstring(string)
	local return_table = {} -- empty table represented by "{}"
	for val in string.gmatch(string,"%w+") do
		table.insert(return_table,val)
	end
	return return_table
end

function love.load() -- "function" keyword when declaring the function (origin of "eof" error)
	stringtable = splitstring("stat;354;332;0.6;0;stalactite_01")
end

function love.draw() -- "function" keyword when declaring the function (origin of "eof" error)
	for n=1,#stringtable,1 do -- lua tables start from 1 by default
		love.graphics.print(stringtable[n],32,32+n*32,0,1,1)
	end
end

Re: <eof> error - using string.gmatch()

Posted: Fri Mar 30, 2012 9:57 am
by timmeh42
Ah, I completely forgot about string being reserved.
Wojak wrote:local return_table = {} -- empty table represented by "{}"
...
function love.load()
Well that's embarrassing :|. I seriously have no idea how those happened.

EDIT: Ok, I'm dumb. Fixed completely.

Re: [solved, stupid programmer me]<eof> error - string.gmatc

Posted: Fri Mar 30, 2012 6:42 pm
by kikito
You can find lots of ways to split strings here - http://lua-users.org/wiki/SplitJoin