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...
[solved] User database storage method
Started by Rsstn, Aug 06 2012 06:05 PM
11 replies to this topic
#1
Posted 06 August 2012 - 06:05 PM
#4
Posted 06 August 2012 - 06: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") endCheck out http://lua-users.org/wiki/ForTutorial if you need more info.
#5
Posted 06 August 2012 - 06:28 PM
well you can loop trough an table like this :
for x=1,#table do print(table[x]) endthis will print it
#7
Posted 06 August 2012 - 06:34 PM
No, the # is the lenght operator in lua, wich gets the lenght/size of strings and tables.
#8
Posted 06 August 2012 - 06:50 PM
Thanks guys, your ideas have made my life much easier!
#9
Posted 06 August 2012 - 07: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?
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?
#10
Posted 06 August 2012 - 07:33 PM
Rsstn, on 06 August 2012 - 07:14 PM, said:
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?
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?
function table.contains(table, id) for _, cont in pairs(table) do if cont == id then return true end end return false endand 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
#11
Posted 07 August 2012 - 05:20 PM
Probably a nooby question, but what is the underscore for?
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











