157 posts
Posted 07 June 2013 - 12:40 PM
I have three programs
1 called userdata which is empty
2 called data:
users = {
{usr = "augustas656", psw = "devlol5"},
{usr = "administrator", psw = "lolol"}
}
3 called main here:
http://pastebin.com/3humFYRkAnd I need help in 3 things (If you can only help me with 1 that's fine)
1 fix my mistakes (all)
2 How to make my code to work for a register/login system that uses FS API
3 An efficient example of a good registration and login system :)/>
8543 posts
Posted 07 June 2013 - 12:45 PM
Split into new topic.
Edit: Your pastebin links both led to the same paste. It's only four lines, so I copied it into the post for you. It's best to put the code in the post itself if it is less than a couple hundred lines or so. Please update with the link to the other paste.
375 posts
Location
USA
Posted 07 June 2013 - 12:47 PM
You posted the same link for 2 and 3.
157 posts
Posted 07 June 2013 - 12:50 PM
Sorry, I actually remember not doing that, maybe I did, the link three is
http://pastebin.com/3humFYRk
375 posts
Location
USA
Posted 07 June 2013 - 12:58 PM
It might be more elegant to read and write the file with textutils serialization. So for instance, when you want to save a new user, read the users file and deserialize it with textutils.deserialize. Then, write the user into the table and serialize it back into the file with textutils.serialize.
157 posts
Posted 07 June 2013 - 01:01 PM
Umm, I have tried serialization at one point, I got a few problems, I went through atleast a few different methods… Could you maybe give me an example of code oh how I could do that, I mean I'm not so new to computercraft and I'm not a newbie to it either, but even if I do get something right it's really inefficient.
375 posts
Location
USA
Posted 07 June 2013 - 01:15 PM
Just wrote a simple untested example. Hopefully this clears things up for you.
local h
local data
if not fs.exists("users") then
-- If it does not exist, we write an empty table.
data = textutils.serialize({ })
h = fs.open("users", "w")
h.write(data)
h.close()
else
h = fs.open("users", "r")
data = h.readAll()
h.close()
local users = textutils.deserialize(data)
-- Now you have the table. We can write to it.
table.insert(users, {usr = "MahUsername", pwd = "cake"})
-- Reserialize
h = fs.open("users", "w")
data = textutils.serialize(users)
h.write(data)
h.close()
end
157 posts
Posted 07 June 2013 - 01:36 PM
You can't write in read mode… and you need to close it I THINK before you can do fs.open again… and how do you insert into a table which is inside a table?
375 posts
Location
USA
Posted 07 June 2013 - 01:45 PM
Fixed. And yes, you can insert a table into a table. :P/>
157 posts
Posted 07 June 2013 - 01:46 PM
How?
for example
table = {{a = 1, b = 2},
{a = 5, b = 7},
{a = 3, b = 6}
}
how do I table insert to change table[1]'s a
375 posts
Location
USA
Posted 07 June 2013 - 01:49 PM
For example:
local t = { }
table.insert(t, {1, 2, 3})
table.insert(t, {4, 5, 6})
print(t[1][2]) -- > 2
print(t[1][3]) -- > 3
print(t[2][2]) -- > 5
7083 posts
Location
Tasmania (AU)
Posted 07 June 2013 - 06:28 PM
Note that table.insert always ADDS things. You cannot use it to "change table[1]'s a". To do that, you'd use table[1].a = <newvalue>.
1583 posts
Location
Germany
Posted 07 June 2013 - 06:38 PM
@Yevano
It's textutils.unserialize…
7083 posts
Location
Tasmania (AU)
Posted 07 June 2013 - 06:40 PM
(And "data" will need to be reserialised regardless of whether the file existed or not - may be easier to only store the table in "data" and pass the serialised string directly to the write function!)
157 posts
Posted 08 July 2013 - 03:56 PM
Can someone just give me a good example which is explained in detail in the comments, plz?
and make sure it is working, and without any mistakes e.g.: textutils.unserillize
If anyone can do that please do so :)/>
157 posts
Posted 08 July 2013 - 04:41 PM
Also, could I still have problems understanding how to insert tables within tables, could you give me an example of just inserted variables into a table, then a table with variables in to a table, then a table with variables into a table into another table and one more like so just within one more table (it's confusing) but like
e.g1: {a=1,b=2,c=3}
e.g2: {{a=1,b=2,c=3}}
e.g3: {{{a=1,b=2,c=3}}}
e.g4: {{{{a=1,b=2,c=3}}}}
Same examples but using the table.insert :)/>
157 posts
Posted 08 July 2013 - 04:45 PM
Another thing, yevano's example of the untested script:
local h
local data
if not fs.exists("users") then
-- If it does not exist, we write an empty table.
data = textutils.serialize({ })
h = fs.open("users", "w")
h.write(data)
h.close()
else
h = fs.open("users", "r")
data = h.readAll()
h.close()
local users = textutils.deserialize(data)
-- Now you have the table. We can write to it.
table.insert(users, {usr = "MahUsername", pwd = "cake"})
-- Reserialize
h = fs.open("users", "w")
data = textutils.serialize(users)
h.write(data)
h.close()
end
Had just simple table brackets {}, and It would give me more understanding if you could have the same example written too but with the table being called something, e.g.: instead of "{}" use something like "tableA = {}".
something, and please use more comments "–comment", because this topic, especially this API is extremely confusing to me…
157 posts
Posted 08 July 2013 - 04:54 PM
And yevano's untested script did not work, because it says for the line of code for table.insert it says it is string, and it's meant to be table. Please help, I'm completely lost now :(/>
157 posts
Posted 16 July 2013 - 04:02 PM
Should I start a new topic about my further questions that I've mentioned of the same subject or is someone going to answer??? :mellow:/>