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

fs.open with tables help

Started by _removed, 03 February 2015 - 09:32 PM
_removed #1
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.
valithor #2
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
_removed #3
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.
valithor #4
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
_removed #5
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
InDieTasten #6
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!
MKlegoman357 #7
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.
_removed #8
Posted 04 February 2015 - 05:34 PM
Admins, feel free to lock this as it has been answered.
Lyqyd #9
Posted 04 February 2015 - 06:03 PM
We don't generally lock Ask a Pro threads.
_removed #10
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.