Normanx
Normanx

Reputation: 3

how to fix: attempt to index global "f" (a nil value), LUA I/O text editing

As title says that error appears when executing the following code.

//open the file

local out = io.open('path', 'r') 

//Fetch all lines and add them to a table

local lines = {}
for line in f:lines() do  
    table.insert(lines, line)
end

//close

  out:close() 

//insert line

 table.insert(lines, 8, "test this bullshit\n")

//temporary file

local out = io.open('pathnew', 'w')
for _, line in ipairs(lines) do
    out:write(line)
end

//close temporary

   out:close()

//delete old file (from the first io.open)

os.remove('pathold')

//rename temporary file to the old one (from the first io.open)

os.rename('pathnew', 'pathold')

Upvotes: 0

Views: 395

Answers (1)

brianolive
brianolive

Reputation: 1671

You are opening a file you call out but then try to read lines from a file you call f. f doesn’t exist.

Upvotes: 1

Related Questions