288 posts
Posted 03 February 2015 - 10:32 PM
For my database system I want to have a file with all the users stored in a table. I can open the file handle, however, I am struggling with how to add a user to a table. Would this need to be acheived with serializing? Also I have no code because it is late in England.
1023 posts
Posted 03 February 2015 - 10:37 PM
For my database system I want to have a file with all the users stored in a table. I can open the file handle, however, I am struggling with how to add a user to a table. Would this need to be acheived with serializing? Also I have no code because it is late in England.
Yes the easiest way to do what you are waiting is to serialize the table using something like this.
local exampleTable = {
"valithor2",
"smigger22"
}
local handle = fs.open("users","w")
handle.write(textutils.serialize(exampleTable))
handle.close()
-- then getting the table back out
local handle = fs.open("users","r")
exampleTable = textutils.unserialize(handle.readAll())
handle.close()
Edited on 03 February 2015 - 09:38 PM
288 posts
Posted 03 February 2015 - 10:51 PM
But what if i wanted to add an user to the table? I dont want to be manually be adding the users.
1023 posts
Posted 03 February 2015 - 10:53 PM
But what if i wanted to add an user to the table? I dont want to be manually be adding the users.
After you read the table from the file
--table.insert(exampleTable, value)
-- An example would be
table.insert(exampleTable, "valithor2")
You would most likely have a function to register new users, and have it to where whatever the person uses to register will be what is inserted into the table. Without a example of a table you will be using or code, I can only give small examples of what I think you would want it to look like.
Edited on 03 February 2015 - 09:56 PM
288 posts
Posted 03 February 2015 - 11:19 PM
But what if i wanted to add an user to the table? I dont want to be manually be adding the users.
After you read the table from the file
--table.insert(exampleTable, value)
-- An example would be
table.insert(exampleTable, "valithor2")
You would most likely have a function to register new users, and have it to where whatever the person uses to register will be what is inserted into the table. Without a example of a table you will be using or code, I can only give small examples of what I think you would want it to look like.
Hmm ok Could you tell me if this would be efficient?
local usersRaw = {}
local function updateUsers()
usersUpdate = fs.open("users", w)
usersUpdate.write(textutils.serialize(usersRaw))
usersUpdate.close()
end
local function addUser(user)
if user:len() > 20 then
nUser = user:sub(1, 20)
table.insert(usersRaw, nUser)
else
table.insert(usersRaw, user)
end
updateUsers()
end
local function retreiveUsers()
local usersFile = fs.open(userPath, "r")
usersRetreive = textutils.unserialize(usersFile.readAll())
userFile.close()
return usersRetreive
end
Edited on 03 February 2015 - 10:20 PM
355 posts
Location
Germany
Posted 03 February 2015 - 11:26 PM
But what if i wanted to add an user to the table? I dont want to be manually be adding the users.
After you read the table from the file
--table.insert(exampleTable, value)
-- An example would be
table.insert(exampleTable, "valithor2")
You would most likely have a function to register new users, and have it to where whatever the person uses to register will be what is inserted into the table. Without a example of a table you will be using or code, I can only give small examples of what I think you would want it to look like.
Hmm ok Could you tell me if this would be efficient?
local usersRaw = {}
local function updateUsers()
usersUpdate = fs.open("users", w)
usersUpdate.write(textutils.serialize(usersRaw))
usersUpdate.close()
end
local function addUser(user)
if user:len() > 20 then
nUser = user:sub(1, 20)
table.insert(usersRaw, nUser)
else
table.insert(usersRaw, user)
end
updateUsers()
end
local function retreiveUsers()
local usersFile = fs.open(userPath, "r")
usersRetreive = textutils.unserialize(usersFile.readAll())
userFile.close()
return usersRetreive
end
Well I would suggest you to keep the style consistant, so that your
retrIEveUsers function directly writes the contents to your
usersRaw table ;)/>
But yes, this should work just fine!
1140 posts
Location
Kaunas, Lithuania
Posted 04 February 2015 - 09:53 AM
Code wise I only see one thing that could be shortened:
local function addUser(user)
table.insert(usersRaw, user:sub(1, 20))
updateUsers()
end
Also, not all of your variables are local.
288 posts
Posted 04 February 2015 - 05:34 PM
Admins, feel free to lock this as it has been answered.
8543 posts
Posted 04 February 2015 - 06:03 PM
We don't generally lock Ask a Pro threads.
288 posts
Posted 04 February 2015 - 06:22 PM
We don't generally lock Ask a Pro threads.
Oh sorry if I wasted your time. I didn't mean to.