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

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
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

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

Post 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'
Attachments
main.love
(319 Bytes) Downloaded 75 times
Last edited by timmeh42 on Fri Mar 30, 2012 10:01 am, edited 1 time in total.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

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

Post 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.
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

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

Post 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
User avatar
timmeh42
Citizen
Posts: 90
Joined: Wed Mar 07, 2012 7:32 pm
Location: Cape Town, South Africa

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

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post by kikito »

You can find lots of ways to split strings here - http://lua-users.org/wiki/SplitJoin
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 75 guests