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

index expected, got nil

Started by mistamadd001, 10 February 2015 - 06:00 PM
mistamadd001 #1
Posted 10 February 2015 - 07:00 PM
I am trying to save an array to file using serialize, as part of this i am first checking to see if there is already an array saved to the file, if so, reading, unserializing and appending it, however I am currently trying to get the program to create an array where there isnt one already (creating the file rather than appending it). I get an error on line 54 (writing the cID into the array, I believe it is due to dataLength being a nil value despite being addressed earlier, can someone please provide a fix.

http://pastebin.com/f999Uzw8

regards

mistamadd001
InDieTasten #2
Posted 10 February 2015 - 07:04 PM
you are ising dataLength out of the scope. Try adding the line
local dataLength = 1
between line 48 and 49; then you can even leave out your else block
mistamadd001 #3
Posted 10 February 2015 - 07:27 PM
http://pastebin.com/Eg88cFeL

new code with changes, still erroring same spot same error
InDieTasten #4
Posted 10 February 2015 - 07:36 PM
Are you sure? I mean… Can you provide error message and new line number?
mistamadd001 #5
Posted 10 February 2015 - 07:41 PM
line 53: index expected, got nil
InDieTasten #6
Posted 10 February 2015 - 07:46 PM
I currently unable to test myself, but it's almost like impossible to produce this exception in your current code. the only thing left to try out is commenting out line 51 for test purpuses
mistamadd001 #7
Posted 10 February 2015 - 07:52 PM
just tried that and no joy, this is getting weird
Edited on 10 February 2015 - 06:55 PM
InDieTasten #8
Posted 10 February 2015 - 08:10 PM
try it out by hardcoding the index. just replace dataLength by 1, just to test, whether the error is actually caused by this exact index
Quintuple Agent #9
Posted 10 February 2015 - 08:13 PM
Ok, I tested the script and noticed that since i had no cids and the file was created blank, textutils.unserialize returned nil, so changing

if data ~= nil then
    dataLength = #data + 1
end
to

if data ~= nil then
    dataLength = #data + 1
else
    data={}
end
worked, also you need to add file.close() after you write the serialized table to file after line 55


Edit: ahhh, the formatting of
 is being finicky with the indents.
[b]Edit2: [/b]Fixed the indents, kinda.
I just gave up and used spaces.
Edited on 10 February 2015 - 07:17 PM
InDieTasten #10
Posted 10 February 2015 - 08:39 PM
Yeah, I was expecting more exceptions afterwards, but the "index expected, got nil" just didn't seem to go away. If data was nil, it would've said: Attempt to index (a nil value), or am I going completely crazy und stupid right now?

If the code works for Quin now, I would assume he wasn't testing the updated code correctly or I'm going insane :D/>

And by he I mean mistamadd001
Quintuple Agent #11
Posted 10 February 2015 - 08:49 PM
I tested the updated code (and uncommented the dataLength = #data + 1) I also redid the cID, cName get to static strings just for testing sake

http://pastebin.com/QueZJQPz

Worked fine, or atleast it worked how i believe it should have. a file was written to clients/cids with 'test' in the serialized table
Edited on 10 February 2015 - 07:54 PM
Quintuple Agent #12
Posted 10 February 2015 - 09:05 PM
Oh, I just noticed something, you have the file clients/cids being written to and closed on lines 43 and 44, thus creating a blank file, then you read the file, so you will only ever have 1 client saved at a time.
ex:
Create client 'a'
file client/a is created
file client/cids is opened in write mode (thus clearing the file and rewriting the whole thing)
file client/cids is closed with nothing written to it
file client/cids is opened in read mode (always blank)
then the file is written to with the new table, only 1 entry

you should move that part of the code over to where you have to check if the 'clients' dir exists, and create cids at the same time

I have edited the pastebin link I gave with the fix to this problem
Edited on 10 February 2015 - 08:25 PM
mistamadd001 #13
Posted 10 February 2015 - 09:49 PM
ah yea, i would have figured that out eventually, thanks, as for the rest, ill have a quick look at the code and see how it goes.

I have a feeling 1 thing that youre doing isnt quite what I want but ill play

thanks for the help so far.
mistamadd001 #14
Posted 10 February 2015 - 09:57 PM
I found a simpler way and less codey too


	local file = fs.open('clients/cids','r')
	local data = file.readAll()
	file.close()
	data = textutils.unserialize(data)
	if data == nil then
	  data = {}
	end
	table.insert(data,cID)
	local file = fs.open('clients/cids','w')
	file.write(textutils.serialize(data))
	print('Client Install Successful')
	rednet.send(cID, 'done')
Edited on 10 February 2015 - 08:58 PM
mistamadd001 #15
Posted 10 February 2015 - 10:07 PM
so its not throwing errors now (YAY), but its still not actually saving the cID into an array then as a string in the cids file… any ideas??

I've updated my most recent pastebin for you to look at
Edited on 10 February 2015 - 09:10 PM
valithor #16
Posted 10 February 2015 - 10:11 PM
so its not throwing errors now (YAY), but its still not actually saving the cID into an array then as a string in the cids file… any ideas??

I've updated my most recent pastebin for you to look at

In the little snippet of code you posted right above your previous post you never close the file, and if you dont close the file it doesnt actually save. You need to do a file.close() after the file.write().
mistamadd001 #17
Posted 10 February 2015 - 10:31 PM
so its not throwing errors now (YAY), but its still not actually saving the cID into an array then as a string in the cids file… any ideas??

I've updated my most recent pastebin for you to look at

In the little snippet of code you posted right above your previous post you never close the file, and if you dont close the file it doesnt actually save. You need to do a file.close() after the file.write().

thank you, that was the final thing, it works perfectly, now to use it in my other programs lol