love.filesystem.lines

Iterate over the lines in a file.

Function

Synopsis

iterator = love.filesystem.lines( name )

Arguments

string name
The name (and path) of the file

Returns

function iterator
A function that iterates over all the lines in the file, returning the line with newlines stripped (if the line ends with \r\n, both are stripped independently of the OS)

Examples

Load highscore list

local highscores = {}
for line in love.filesystem.lines("highscores.lst") do
  table.insert(highscores, line)
end

Load tab separated values

local function getList (stringLine)
	local list = {}
	for value in (string.gmatch(stringLine, "[^%s]+")) do  -- tab separated values
		if type(tonumber (value)) == "number" then
			table.insert (list, tonumber (value))
		elseif value == "true" then
			table.insert (list, true)
		elseif value == "false" then
			table.insert (list, false)
		elseif value == "nil" then
			table.insert (list, nil) -- do nothing :(
		else
			table.insert (list, value)
		end
	end
	return list
end

function loadTSV (filename)
	local lines = love.filesystem.lines(filename..".tsv") -- open file as lines
	local tabl = {}
	for line in lines do -- row iterator
		local list = getList (line)
		if #list == 2 then
			tabl[list[1]] = list[2]
			print (list[1], list[2])
		elseif #list > 2 then
			local list2 = {}
			for i = 2, #list do
				table.insert (list2, list[i])
			end
			tabl[list[1]] = list2
			print (list[1], table.concat2 (list2, ', '))
		end
	end
	return tabl
end

See Also


Other Languages