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

Attempt To Index ?

Started by Kaleb702, 17 February 2012 - 04:37 AM
Kaleb702 #1
Posted 17 February 2012 - 05:37 AM
http://pastebin.com/rfejBmri
I'm probably f***ing up with io.open too much.
Espen #2
Posted 17 February 2012 - 11:37 AM
You're opening and reading the same file multiple times.
Every time you do an "io.open" on a file, you create another file handle.
But instead of doing this…
if io.open(shell.resolve(".") .. "coo.kie","r") ~= nil then
…try to assign it to a variable first and then check that variable instead, like e.g.:
local hCookie = io.open(shell.resolve(".") .. "coo.kie","r")
if hCookie then
This will open the file "coo.kie" and assign its file-handle/-reference to the variable hCookie.
Now you can do multiple checks with hCookie, which always references the same file-handle instead of creating new handles over and over again.

Then to read the content of the file use hCookie:read("*all").
But make sure to assign that content to a variable as well!
local cookieContent = hCookie:read("*all")
Once you've read the content from the file you can't read it again, at least not with the same handle.
Just think about what happens when you read a file line-for-line. Every time you try to read a line it reads the next line and doesn't start from the beginning of the file.

I didn't go into the program logic any further, if you've fixed the above and you still get errors, just give a holler. :D/>/>
Kaleb702 #3
Posted 18 February 2012 - 01:22 AM
http://pastebin.com/EZCiimeb
^ New URL due to my stupidity.


Well, that fixed it, thanks.

Now the only issue is that when I close it, it corrupts mycounter.sif. How did I fail now?
Espen #4
Posted 18 February 2012 - 03:10 AM
No problem. You're not stupid if you're trying to understand and learn something.
Nobody was born with knowledge, we all had to get it from somewhere in some way. ^_^/>/>

About your problem:
You're still reading a file multiple times.
If you do this:
myfile:read("*all")
Then you've read the file (of handle "myfile") already and can't read it again.
You'll only get nil back, because you're at the end of the file.

Just imagine that, when you opened a file, then there's a pointer at the beginning of the file.
Now as soon as you read a line, a number, etc. then the pointer is moved behind the data you've read from the file.
That means if you again read a line or a number or sth., then the pointer continues from the point you left off.
Now if you've read everything within a file, then the pointer will be at the end of the file and the next time you try to read another line or number, etc. from the file, then of course you'll only get "nil" from then on, because there isn't any data left where the pointer is at.
Some thing goes if you read the file in one go using myfile:read("*all").
After that you can't just do another read("*all") on the same handle "myfile".
To read it again you would have to open it again using another file handle.

So instead of trying to read it multiple times, read it once and store the read data into a variable, e.g.:

local hFile	 -- Handle, that will hold the reference to the opened file.
local fileContent   -- We will read the whole file in one go into this variable.

hFile = io.open("somefile", "r")	-- Load the file for reading.
fileContent = hFile:read("*all")	-- Read the whole file into 'fileContent'
hFile:close() -- We have read the whole file into 'fileContent', so we do not need the file handle anymore.

-- From now on we will continue to work with the data in 'fileContent'

Basic example if you want to read a file line-for-line and operate on the line as soon as it is read:

local hFile = io.open("somefile", "r")

while true do
	local line = io.read()
	if line == nil then break end   -- If the last line read is nil, then we have reached the end of the file and this break out of the loop.
	-- Do something with the line here.
end

Edit: Removed contractions from some comments to ensure better syntax highlighting.
Edited on 18 February 2012 - 01:10 PM
Kaleb702 #5
Posted 18 February 2012 - 04:54 AM
Been fixed, thanks. Will upload in a while.