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

Tabelize Labels.txt

Started by JamiePhonic, 11 August 2013 - 05:18 PM
JamiePhonic #1
Posted 11 August 2013 - 07:18 PM
This is a small program i wrote to convert the labels.txt file into a serialized table that can then be used for whatever.

i wrote it because all the computers i have are named and some of them run the client side code for my computercraft mainframe which needs to be able to tell the user what computer they are on, and which server/relay they are connected to, but more importantly, tell the server which command came from where. below is the pastebin link and some example code.

Pastebin: Nn2xNY8x

Example code:

File = fs.open("rom/WhoIs", "r")
Table = File.readAll()
WhoIs = textutils.unserialize(Table)
File.close() --close the file
if WhoIs == nul then
error("whois file is empty")
end
MyName = WhoIs[os.computerID()] --set myname to the value associated with os.computerID() (e.g. 1)
if MyName == "" then -- if myname is empty
MyName = "Unknown" --then set myname to unknown
end
--end of that bit
clear()
sleep(1)
print("This is computer: ", os.computerID(), " (", MyName, ")")

if you are going to use this in the way i am (the client software is in the rom folder so people cant edit it and i don't need to manually change 50 copies of the code if i make changes) then having the whois file in the rom folder is problem because this program wont be able to update the whois file.

the solution? create a symlink to the whois file on the computer of your choice :)/>
theoriginalbit #2
Posted 16 August 2013 - 06:20 PM
Two things…

1. Have you actually tested this code? Because if you did you would have noticed that a computer does not have access to the "labels.txt" file… If it did, it would also have access to other computer's files.
2. You have a few bugs in your example code:
2a. The first is a compile bug… "if WhoIs == nul" … nope, nil.
2b. The next is your logic for MyName… you do "if MyName == "" then" well if you don't have an entry in the WhoIs table, it won't be "", it'll be `nil`. So you need to change that to "if not MyName then"
2c. The function `clear` is not defined in that scope.
NOTE: It is a very good idea that when supplying example code to potential users, that the code be bug free.