Parsing file in Lua for specific value in table -
i new lua programming , trying parse local file on pc , save elements of table/array strings.
i have been able data , print each line of file, except having problems when trying specific value , save them strings or print line. appreciated.
here code sample:
function file_exists(file) local f = io.open(file, "rb") if f f:close() end return f ~= nil end function lines_from(file) if not file_exists(file) return {} end lines = {} line in io.lines(file) lines[#lines + 1] = line end return lines end local file = 'stats.txt' local lines = lines_from(file) k,v in pairs(lines) print('line[' .. k .. ']', v) end
your code works expected. copied code file named temp.lua
. here output:
line[1] function file_exists(file) line[2] local f = io.open(file, "rb") line[3] if f f:close() end line[4] return f ~= nil line[5] end line[6] line[7] line[8] function lines_from(file) line[9] if not file_exists(file) return {} end line[10] lines = {} line[11] line in io.lines(file) line[12] lines[#lines + 1] = line line[13] end line[14] return lines line[15] line[16] end line[17] line[18] local file = 'temp.lua' line[19] local lines = lines_from(file) line[20] line[21] k,v in pairs(lines) line[22] print('line[' .. k .. ']', v) line[23] end
Comments
Post a Comment