How to read numbers?

The read method takes a list of format strings. So to read three numbers from each line, we have:

local f = io.open 'myfile.txt' -- assuming it always exists!
while true do
 local x,y,z = f:read('*n','*n','*n')
 if not x then break end
 ...
end f:close()

For more complicated situations, it is common to use string.match to extract the numbers and explicitly use tonumber to convert them.



Back