Jump to content




fs.open with tables help


  • You cannot reply to this topic
9 replies to this topic

#1 _removed

  • Members
  • 262 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.

#2 valithor

  • Members
  • 1,053 posts

Posted 03 February 2015 - 10:37 PM

 smigger22, on 03 February 2015 - 10:32 PM, said:

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 by valithor, 03 February 2015 - 10:38 PM.


#3 _removed

  • Members
  • 262 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.

#4 valithor

  • Members
  • 1,053 posts

Posted 03 February 2015 - 10:53 PM

 smigger22, on 03 February 2015 - 10:51 PM, said:

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 by valithor, 03 February 2015 - 10:56 PM.


#5 _removed

  • Members
  • 262 posts

Posted 03 February 2015 - 11:19 PM

 valithor, on 03 February 2015 - 10:53 PM, said:

 smigger22, on 03 February 2015 - 10:51 PM, said:

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 by smigger22, 03 February 2015 - 11:20 PM.


#6 InDieTasten

  • Members
  • 357 posts
  • LocationGermany

Posted 03 February 2015 - 11:26 PM

 smigger22, on 03 February 2015 - 11:19 PM, said:

 valithor, on 03 February 2015 - 10:53 PM, said:

 smigger22, on 03 February 2015 - 10:51 PM, said:

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!

#7 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, 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.

#8 _removed

  • Members
  • 262 posts

Posted 04 February 2015 - 05:34 PM

Admins, feel free to lock this as it has been answered.

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 04 February 2015 - 06:03 PM

We don't generally lock Ask a Pro threads.

#10 _removed

  • Members
  • 262 posts

Posted 04 February 2015 - 06:22 PM

 Lyqyd, on 04 February 2015 - 06:03 PM, said:

We don't generally lock Ask a Pro threads.
Oh sorry if I wasted your time. I didn't mean to.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users