How to Read a File in Lua
Reading a file in lua is pretty straight forward.
lines = {}
for line in io.lines(arg[1]) do
lines[#lines + 1] = line
end
for k,v in pairs(lines) do
print(k .. ' ' .. v)
end
First create a map for the lines to be placed in. Then for iterate over the files lines using io.lines
(we have no error checking here, we just assume it works). We use what we know about reading program arguments to get the filename from the command line.
We place each line in the map using the line number as key.
Finally we print out the pairs in lines
having a space between the line number and the actual contents.