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

Todo List Server: Not Sure Whats Going on Here

Started by xxrevanxx, 02 January 2013 - 10:27 PM
xxrevanxx #1
Posted 02 January 2013 - 11:27 PM
Hey so I made this program which would make the computer act as a server awaiting commands, everything works fine except for when you go to delete an item from the list. I don't know much about the fs api so I attempted to avoid using the handle. Every time the deleteList function is run the server won't send the new list to the monitor that displays the to do list.
Server: http://pastebin.com/nhKvUapc/
Monitor: http://pastebin.com/BDexNRKp/
The monitor will display textutils:141: attempt to concatenate string and nil.
theoriginalbit #2
Posted 03 January 2013 - 12:51 AM
Firstly is there a reason that you are using many files, verses one file and a table?
I think it could be much simpler to do that.

Also the issue is most likely because you are always asking for 10 files, what if there is more? what if there is less? when there is less you are getting the nil I'd say…

Lastly why are you sending rednet messages out with questions? why don't you send with the original sent message with the addition of what they want to delete or create.

EDIT:
Here is an example of what I was talking about with the tables. This uses your code, with some changes so the table will work, this was typed in pastebin so not tested and may have some syntax errors (sorry doing some Java and ObjC assignments, so sometimes I cross languages :P/> ) but you should get the general idea. If you want to know about anything just ask :)/>

Server: http://pastebin.com/jN3emYrM
Monitor: http://pastebin.com/apg3D8Te
xxrevanxx #3
Posted 03 January 2013 - 02:58 AM
Thanks I'll try it out :D/>
xxrevanxx #4
Posted 03 January 2013 - 10:24 AM
Yea I tried your server code I believe if caught most of the errors but now the server isn't starting with an error todoserver:13:attempt to index ? (a nil value)

I'm using tekkit version of computer craft which doesn't fully use the io api as far as I know so I changed it to the fs api thinking that was the issue but still returns the same error
http://pastebin.com/X7dbq8VE/
remiX #5
Posted 03 January 2013 - 10:33 AM
Try changing
tTodoList = textutils.unserialize( a.readAll( "*a"))
to just
tTodoList = textutils.unserialize( a.readAll())
with fs I think you just need to do readAll()

edit: fs doesn't return as a table, but as a string so try this to insert the text into a table:


line = a.readLine()
repeat
    table.insert(tTodoList, line)
    line = a.readLine()
until not line
xxrevanxx #6
Posted 03 January 2013 - 10:38 AM
I just put that in same error

EDIT: Where does the edit code line go?
remiX #7
Posted 03 January 2013 - 12:27 PM
Where you try and textutils.unserialize(a.readAll())
theoriginalbit #8
Posted 03 January 2013 - 12:32 PM
Yea I tried your server code I believe if caught most of the errors but now the server isn't starting with an error todoserver:13:attempt to index ? (a nil value)

I'm using tekkit version of computer craft which doesn't fully use the io api as far as I know so I changed it to the fs api thinking that was the issue but still returns the same error
http://pastebin.com/X7dbq8VE/

Yeh that code would work on Tekkit no problems. IO API works fine, I use it all the time, I prefer to use it over the FS API

On line 13 the read("*") actually reads all the contents from the file. I tend to read all the contents of a file then process it, just incase something errors in my other threads and leaves a file handle open (which is more likely when reading line by line vs all at once.

Also remiX, if he used the solution I posted using the textutils.unserialize on the entire file would be fine, since the entire table is dumped straight into the files instead of entry by entry dumped, there would only be one line in the file. I know this works as I use it all the time.
remiX #9
Posted 03 January 2013 - 12:35 PM
Yeah but he opened the file with fs not io. So I told him how you can add the contents to a table with fs. I don't use io, sadly.
theoriginalbit #10
Posted 03 January 2013 - 12:39 PM
Yeah but he opened the file with fs not io. So I told him how you can add the contents to a table with fs. I don't use io, sadly.

True. IO returns a string too though. I don't quite understand that "Tekkit IO" bug that he said tho, I've never encountered it. Also why don't you use IO? If Lua is like every other language, then FS would just inherit IO, since IO in all high level languages I know is the base class…
remiX #11
Posted 03 January 2013 - 12:43 PM
When I started CC I never knew about io and only fs so I guess I'm just use to it now :P/> Also, when I do use io, everything starts to error now and then like the for line in file.lines() do print(line) end, when it hits the last line, it errors with index expected or something silly, and I can never manage to fix it :|
theoriginalbit #12
Posted 03 January 2013 - 12:46 PM
When I started CC I never knew about io and only fs so I guess I'm just use to it now :P/>/> Also, when I do use io, everything starts to error now and then like the for line in file.lines() do print(line) end, when it hits the last line, it errors with index expected or something silly, and I can never manage to fix it :|

That would most likely be due to the fact that the last thing read from a file with IO is EOF or nil, so when using IO I'd have it in a while line = etc etc… The only time I have issues with IO is if a file handle is left open, but get the same problem with fs.
xxrevanxx #13
Posted 03 January 2013 - 12:47 PM
So its reading from file at the beginning however the file is yet to have anything in it therefore returning nil no? So should I remove the readFromFile() which runs before the rest of the code and place it back or is there a way to just return to the main code if the file is empty
theoriginalbit #14
Posted 03 January 2013 - 12:49 PM
So its reading from file at the beginning however the file is yet to have anything in it therefore returning nil no? So should I remove the readFromFile() which runs before the rest of the code and place it back or is there a way to just return to the main code if the file is empty
Oh yes, sorry that is my fault. That is a good idea. But after that you would want it to read so that it can load what you have previously entered.

EDIT: change the contents of the function readFromFile to something like this (making changes as you see fit)


local file = io.open( saveFile, "r" )
local contents = file:read("*a")
file:close()
if contents then -- if not nil
  tTodoList = textutils.unserialize( contents )
end

EDIT 2: Also it may be worth surrounding above in an if fs.exists( saveFile ) before trying to open it for read.
xxrevanxx #15
Posted 03 January 2013 - 01:12 PM
Quick question should I pure make the dir disk/todo
And is it a dir or a file
theoriginalbit #16
Posted 03 January 2013 - 01:13 PM
Quick question should I pure make the dir disk/todo
And is it a dir or a file
In my code, todo is the file in the path disk/ if you want todo to be a folder, add a / and file name to the end of saveFile variable.
theoriginalbit #17
Posted 03 January 2013 - 01:28 PM
Also if you may not have noticed in the code I wrote if there are too many items to display on the screen it just writes and more, that was the nice quick way of doing it. However if you want it to scroll through them all you could make the changes. I'm on my phone so I can't really type heaps of stuff to suggest… So here's a similar one I prepared earlier for someone else…
http://pastebin.com/mqwMPSbB
xxrevanxx #18
Posted 03 January 2013 - 01:41 PM
So it works however if the todoserver resets then it requires me to clear the disk
Edit: Also when going to delete something the server returns with io:27:bad argument: string expected, got table