How to read a text file and store data in a table

advertisements

I want to read data from a txt file, and store it in an array, so I can index it. The text file is something like this:

0.1
0.2
0.3
0.4
0.5
0.6

The data will be stored in an array like temp, so then I can index the specific value by temp(0), can anyone show me how to do that?


The code below is a variation of the one given by Yu Hao. It is simpler and does not leave any file open after the loop: f:lines() does not close the file when the loop ends.

local temp = {}
for line in io.lines("t.txt") do
    table.insert(temp, tonumber(line))
end