This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
valithor's profile picture

[Lua][Error] Attempt to index ? (a nil value)

Started by valithor, 04 June 2013 - 03:35 PM
valithor #1
Posted 04 June 2013 - 05:35 PM
So i am attempting to make a short program that will read a file, if there isn't a file it will make a file and input a series of numbers. This will be used in place of gps in a couple of mining turtle programs i am writing but on line 2 i get "attempt to index ? (a nil value)" i get this while trying to check to see if the first number in the line is a nil value.

r = fs.open("test2","r")
if r.readLine(1)==nil then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end
r = fs.open("test2","r")
x = tonumber(r.readLine(1))
r.close()
x = x+1
s = fs.open("test2","w")
s.writeLine(x)
s.close()
r = fs.open("test2","r")
local x = tonumber(r.readLine(1))
print(x)
s.close()

This is part of the code as i was attempting to get this to work before i actually write the mining program.
Engineer #2
Posted 04 June 2013 - 05:40 PM
readLine doesnt take arguments. It just literally reads the next line, so if line 1 is 'a' and line 2 is 'b' then;

print(r.readLine()) --> a
print(r.readLine()) --> b
valithor #3
Posted 04 June 2013 - 06:00 PM
so then how would i see if there is a number there or not in the file it is reading?
Bomb Bloke #4
Posted 04 June 2013 - 10:05 PM
it still sounds a little like you're confused about what fs.readLine() does.

Anyway, use fs.exists() for this.

if not fs.exists("test2") then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end
valithor #5
Posted 04 June 2013 - 11:02 PM
it still sounds a little like you're confused about what fs.readLine() does.

Anyway, use fs.exists() for this.

if not fs.exists("test2") then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end

Thank you this is what i was looking for

I know that the fs.readLine() would not make a file but i was looking for a way to see if the file existed, I knew i could use the fs.open("test2","w") to make a file but then i would not know if i was erasing the data inside the file itself, But i didnt know a fs.exist command even existed so i was unable to find a way to see if the file existed.

I have never done any kind of coding in any kind before so a lot of it at first looks like a alien language to me
Edited on 04 June 2013 - 09:04 PM
Bomb Bloke #6
Posted 05 June 2013 - 07:41 AM
In that case, keep the API list handy. Whenever you want a command for a certain task, take a look there for the API that seems most closely related, see what functions it has for you.
theoriginalbit #7
Posted 05 June 2013 - 07:47 AM
I have never done any kind of coding in any kind before so a lot of it at first looks like a alien language to me
You should look at a real programming language, then you will see alien :P/>
Oh or take a look at one that looks alien to experienced programmer.

Bomb Bloke is right though, bookmark the API list in the wiki, also bookmarking some tutorials on lua-users.org or the PIL can come in very handy while programming, and lastly, Google is our friend, 9/10 it can probably answer your questions much quicker (/shamelessplug don't know how to Google efficiently, take a look at my tutorial)… even the best of programmers need reference material from time to time. :)/>