Difference between revisions of "love.filesystem.lines"

(Load tab separated values)
(Examples: Less confusing example. Changed separation character from tab to comma. Cleanup.)
Line 1: Line 1:
 
Iterate over the lines in a file.
 
Iterate over the lines in a file.
 +
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 9: Line 10:
 
=== Returns ===
 
=== Returns ===
 
{{param|function|iterator|A function that iterates over all the lines in the file, returning the line with newlines stripped (if the line ends with <code>\r\n</code>, both are stripped independently of the OS)}}
 
{{param|function|iterator|A function that iterates over all the lines in the file, returning the line with newlines stripped (if the line ends with <code>\r\n</code>, both are stripped independently of the OS)}}
=== Examples ===
+
 
== Load highscore list ==
+
== Examples ==
 +
 
 +
=== Load highscore list ===
 
<source lang="lua">
 
<source lang="lua">
 
local highscores = {}
 
local highscores = {}
for line in love.filesystem.lines("highscores.lst") do
+
for line in love.filesystem.lines("highscores.txt") do
   table.insert(highscores, line)
+
   table.insert(highscores, tonumber(line))
 
end
 
end
 
</source>
 
</source>
  
== Load tab separated values ==
+
=== Load comma separated values ===
 +
<source lang="lua">
 +
local function splitCsvLine(line)
 +
local values = {}
  
<source lang="lua">
+
for value in line:gmatch("[^,]+") do -- Note: We won't match empty values.
local function getList (stringLine)
+
-- Convert the value string to other Lua types in a "smart" way.
local list = {}
+
if     tonumber(value) then table.insert(values, tonumber(value)) -- Number.
for value in (string.gmatch(stringLine, "[^%s]+")) do -- tab separated values
+
elseif value == "true" then table.insert(values, true)           -- Boolean.
if type(tonumber (value)) == "number" then
+
elseif value == "false" then table.insert(values, false)           -- Boolean.
table.insert (list, tonumber (value))
+
else                         table.insert(values, value)           -- String.
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
 
end
 
end
return list
+
 
 +
return values
 
end
 
end
  
function loadTSV (filename)
+
local function loadCsvFile(filename)
local lines = love.filesystem.lines(filename..".tsv") -- open file as lines
+
local csv = {}
local tabl = {}
+
for line in love.filesystem.lines(filename) do
for line in lines do -- row iterator
+
table.insert(csv, splitCsvLine(line))
local list = getList (line)
 
if #list == 2 then
 
tabl[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
 
end
 
 
end
 
end
return tabl
+
return csv
 +
end
 +
 
 +
--[[ cool.csv:
 +
Foo,Bar
 +
true,false,11.8
 +
]]
 +
local csv = loadCsvFile("cool.csv")
 +
for row, values in ipairs(csv) do
 +
print("row="..row.." count="..#values.." values=", unpack(values))
 
end
 
end
 
</source>
 
</source>
  
 
== See Also ==
 
== See Also ==
 +
* [[love.filesystem.read]]
 
* [[parent::love.filesystem]]
 
* [[parent::love.filesystem]]
 +
 
[[Category:Functions]]
 
[[Category:Functions]]
 
{{#set:Description=Iterate over the lines in a file.}}
 
{{#set:Description=Iterate over the lines in a file.}}
 
{{#set:Since=000}}
 
{{#set:Since=000}}
 +
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.filesystem.lines}}
 
{{i18n|love.filesystem.lines}}

Revision as of 09:55, 13 August 2022

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.txt") do
  table.insert(highscores, tonumber(line))
end

Load comma separated values

local function splitCsvLine(line)
	local values = {}

	for value in line:gmatch("[^,]+") do -- Note: We won't match empty values.
		-- Convert the value string to other Lua types in a "smart" way.
		if     tonumber(value)  then  table.insert(values, tonumber(value)) -- Number.
		elseif value == "true"  then  table.insert(values, true)            -- Boolean.
		elseif value == "false" then  table.insert(values, false)           -- Boolean.
		else                          table.insert(values, value)           -- String.
		end
	end

	return values
end

local function loadCsvFile(filename)
	local csv = {}
	for line in love.filesystem.lines(filename) do
		table.insert(csv, splitCsvLine(line))
	end
	return csv
end

--[[ cool.csv:
Foo,Bar
true,false,11.8
]]
local csv = loadCsvFile("cool.csv")
for row, values in ipairs(csv) do
	print("row="..row.." count="..#values.." values=", unpack(values))
end

See Also


Other Languages