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

[solved] User database storage method

Started by Rsstn, 06 August 2012 - 05:05 PM
Rsstn #1
Posted 06 August 2012 - 07:05 PM
HI, guys.
I'm in the process of making a set of programs that will use rednet to provide customer services for customers on a server.
Now, once the central PC receives the id of a new computer, it must add it to the database.
The part that is puzzling me is how to store the data. A table seems sensible, but I need to be able to send the same message to every computer in the database and their IDs won't be in any particular order (so it will look like 4, 65, 165, 64, 22, 34 etc.).
I tried using a table for storing the IDs, but had trouble using the FS API to save it to a file. Is this a known problem, or am I doing something wrong?
Also, does anyone have any ideas on how to retrieve the IDs from the table for sending?
I thought of using a 'for' loop to send the message to the ID stored in each row of the table, one by one, until it reaches a row that has no value (the end of the database).
Any thoughts/ideas on how I could make this work efficiently? I fell like my ideas at the moment will make a long and slow program…
Kolpa #2
Posted 06 August 2012 - 07:18 PM
try using
textutils.serialize(table)
and
textutils.unserialize(string)
as shown Here
Rsstn #3
Posted 06 August 2012 - 07:24 PM
try using
textutils.serialize(table)
and
textutils.unserialize(string)
as shown Here

Yeah…
I wish I'd thought of that first! That will make saving the table easier.
Any ideas as alternatives to the loop for sending?
inventor2514 #4
Posted 06 August 2012 - 07:26 PM
For loops are fairly flexible. There are several options for accessing every element until a nil value is found. Here are two examples:

-- Example A
for i=1, #table do
rednet.send(table[i], "a message")
end
-- Example B
for _, id in ipairs(table) do
rednet.send(id, "a message")
end
Check out http://lua-users.org/wiki/ForTutorial if you need more info.
Kolpa #5
Posted 06 August 2012 - 07:28 PM
well you can loop trough an table like this :

for x=1,#table do
print(table[x])
end
this will print it
Rsstn #6
Posted 06 August 2012 - 07:30 PM
When you say:


 #table

Would I fill in that with the number of rows in the table? If so, I'd need some way to find out, as the table will increase all the time!
MysticT #7
Posted 06 August 2012 - 07:34 PM
No, the # is the lenght operator in lua, wich gets the lenght/size of strings and tables.
Rsstn #8
Posted 06 August 2012 - 07:50 PM
Thanks guys, your ideas have made my life much easier!
Rsstn #9
Posted 06 August 2012 - 08:14 PM
Ok, so after some thinking, I've found a problem…
Some users may register their PC twice. If this happens, they will receive the message twice. Not a major problem, but it's annoying. Any ideas?
Kolpa #10
Posted 06 August 2012 - 08:33 PM
Ok, so after some thinking, I've found a problem…
Some users may register their PC twice. If this happens, they will receive the message twice. Not a major problem, but it's annoying. Any ideas?
use this function to test if the id is already in the table

function table.contains(table, id)
  for _, cont in pairs(table) do
	if cont == id then
	  return true
	end
  end
  return false
end
and run it with an if before writing to the table like this:

users = {}
if table.contains(users,id) then
print(id.." tried to double register")
else
table.insert(users,id)
end
Rsstn #11
Posted 07 August 2012 - 06:20 PM
Probably a nooby question, but what is the underscore for?
Kolpa #12
Posted 10 August 2012 - 06:33 PM
Probably a nooby question, but what is the underscore for?
its a dummy value, you don't need it in the script but you have to retrieve it to get the second value