fs.open with tables help
#1
Posted 03 February 2015 - 10:32 PM
#2
Posted 03 February 2015 - 10:37 PM
smigger22, on 03 February 2015 - 10:32 PM, said:
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
Posted 03 February 2015 - 10:51 PM
#4
Posted 03 February 2015 - 10:53 PM
smigger22, on 03 February 2015 - 10:51 PM, said:
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
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:
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.
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
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:
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.
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
But yes, this should work just fine!
#7
Posted 04 February 2015 - 09:53 AM
local function addUser(user) table.insert(usersRaw, user:sub(1, 20)) updateUsers() end
Also, not all of your variables are local.
#8
Posted 04 February 2015 - 05:34 PM
#9
Posted 04 February 2015 - 06:03 PM
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











