Edited by deadaccount, 28 June 2023 - 12:29 AM.
1
file2table
Started by deadaccount, Jul 25 2018 08:14 PM
2 replies to this topic
#1
Posted 25 July 2018 - 08:14 PM
.
#2
Posted 25 July 2018 - 08:35 PM
Neat! One small improvement you could make would be to use a for loop to iterate over the lines instead - it makes it a wee bit cleaner:
It may it'd also be nicer if readFile and loadFiles returned a table instead of mutating their argument. This means you don't have to worry about clearing the rest of the table, and allows you can do something like:
local index = 1 for l in h.readLine do table[index] = l index = index + 1 end
It may it'd also be nicer if readFile and loadFiles returned a table instead of mutating their argument. This means you don't have to worry about clearing the rest of the table, and allows you can do something like:
local contents = readFile("foo.lua") --# Instead of local contents = {} readFile("foo.lua", contents)
Edited by SquidDev, 25 July 2018 - 08:35 PM.
#3
Posted 26 July 2018 - 02:43 AM
You could furthermore have the for loop make use of io.lines(), which'd save you from having to open and close your file handles manually:
Also, rather than storing two versions of each file, you might consider:
tbl.content() would then get you the full string, and would remain up to date even if you modified some of the lines.
Note that you'd need to rename your "table" parameter to something like "tbl" (or whatever else), so as not to override and block access to the pre-existing table API (which contains the "concat" function).
for l in io.lines(path) do
Also, rather than storing two versions of each file, you might consider:
tbl["content"] = function() table.concat(tbl, "\n") end
tbl.content() would then get you the full string, and would remain up to date even if you modified some of the lines.
Note that you'd need to rename your "table" parameter to something like "tbl" (or whatever else), so as not to override and block access to the pre-existing table API (which contains the "concat" function).
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users