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

Creating a List of Numbers/Strings

Started by areuz, 20 March 2014 - 03:45 PM
areuz #1
Posted 20 March 2014 - 04:45 PM
I'm trying to figure out a way how to create a list of computer ID's for my Computer Network. Right now, I have figured out how to tell another computer, the other computers Id and a unique password, now I would like to somehow save that Computer Id into a list, and be able to read it line by line. I searched the internet for an hour, but didn't find an easy enough answer.
Thanks in advance :)/>
Lyqyd #2
Posted 20 March 2014 - 05:27 PM
Moved to Ask a Pro.
Bomb Bloke #3
Posted 20 March 2014 - 07:27 PM
Save it into a temporary list that lasts the duration of the program, or a permanent copy to the computer's drive…?
Lua.is.the.best #4
Posted 20 March 2014 - 11:17 PM
I agree with Bomb Bloke.

fs.makeDir(TempProgramFiles)
shell.setDir(TempProgramFiles)
fs.open("tempfile.tmp","w")
fs.writeLine("-LISTSTART-")
fs.writeLine("Listitem")
fs.writeLine("-LISTEND-")
fs.close()
is all you need to write.
Then, at the end, do:

shell.setDir(..)
shell.setDir(TempProgramFiles)
fs.delete("tempfile.tmp")

Bomb Bloke #5
Posted 21 March 2014 - 08:22 AM
To be clear, those weren't suggestions in my post - I don't "get" which of those two separate tasks FireDust is asking about.
oeed #6
Posted 21 March 2014 - 09:34 AM
All you need to do is put them in to a table, use something like this:


--create the table (list)
local computerIDs = {}
--each time you receive an id, do this
table.insert(computerIDs, id)
--loop over all ids (if you want to do it)
for i, id in ipairs(computerIDs) do
print('ID: '..tostring(id))
end

If you want to save/load the table you'll need to serialise it:

--load, note, you will need to save the file at least once first
local h = fs.open('ids', 'r')
if h then
	local computerIDs = textutils.unserialize(h.readAll())
	h.close
end
--saving
local h = fs.open('ids', 'w')
if h then
	h.write(textutils.serialize(computerIDs)
	h.close()
end

For more info about tables check out this tutorial.
Edited on 21 March 2014 - 08:35 AM