Save table value

4

I'd like to save the value assigned to a table in real time, I'll explain.

I have 2 files:

Main.lua

dofile("tester.lua")

io.write("blabla")

table.insert(oi, io.read())

print(oi[1])

io.read()

tester.lua

oi = {}

If I execute and write: kkk will return - > lol But when you close the program, it does not save to the file. How do I make the value added to the table continue to be saved?

    
asked by anonymous 17.08.2014 / 22:51

1 answer

5

It may be interesting to open the file in a variable and edit the file by it at the end of the run:

dofile("tester.lua")
arquivo = assert(io.open("tester.lua", "w"), "Falha ao abrir arquivo") -- O assert é apenas caso possa dar algum erro, mas caso prefira, descomente a linha abaixo e comente essa!
--arquivo = io.open("tester.lua", "w")
io.write("blabla")
table.insert(oi, io.read())
print(oi[#oi])

texto = "oi = {"
for i = 1, #oi do
    texto = texto.."\""..tostring(oi[i]).."\""
    if not (i == #oi) then
        texto = texto..", "
    end
end
texto = texto.."}"

arquivo:write(texto)
arquivo:flush()
io.close(arquivo)
io.read()

In this way, it inserts in the table the data that is passed at the prompt.

    
18.08.2014 / 00:43