Difference between revisions of "love.filesystem.lines"
(→Example) |
(→Load tab separated values) |
||
Line 46: | Line 46: | ||
if #list == 2 then | if #list == 2 then | ||
tabl[list[1]] = list[2] | tabl[list[1]] = list[2] | ||
− | |||
elseif #list > 2 then | elseif #list > 2 then | ||
local list2 = {} | local list2 = {} | ||
Line 53: | Line 52: | ||
end | end | ||
tabl[list[1]] = list2 | tabl[list[1]] = list2 | ||
− | |||
end | end | ||
end | end |
Revision as of 22:59, 10 August 2022
Iterate over the lines in a file.
Contents
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]
elseif #list > 2 then
local list2 = {}
for i = 2, #list do
table.insert (list2, list[i])
end
tabl[list[1]] = list2
end
end
return tabl
end
See Also
Other Languages
Dansk –
Deutsch –
English –
Español –
Français –
Indonesia –
Italiano –
Lietuviškai –
Magyar –
Nederlands –
Polski –
Português –
Română –
Slovenský –
Suomi –
Svenska –
Türkçe –
Česky –
Ελληνικά –
Български –
Русский –
Српски –
Українська –
עברית –
ไทย –
日本語 –
正體中文 –
简体中文 –
Tiếng Việt –
한국어
More info