How to compare data of a table
Started by Alerith, Feb 24 2013 01:31 PM
11 replies to this topic
#1
Posted 24 February 2013 - 01:31 PM
That. How to compare data (I.e. users) in a table with other data (i.e. passwords) in other table?
I made one code with register and login but I cannot make it work
Thanks in advance
I made one code with register and login but I cannot make it work
Thanks in advance
#2
Posted 24 February 2013 - 01:37 PM
Hmm i had one once. all i did was something like this and it worked. Make two tables next to each other. one on top the other on the bottom. use users = {"user1", "user2") and under that use pass = {"pass1", "pass2"} when i did it with this program that worked.
#4
Posted 24 February 2013 - 01:45 PM
#5
Posted 24 February 2013 - 03:49 PM
By looking at your code I see what you're trying to accomplish, so I rewrote it slightly.
This way will allow you to have as much users as you want, but when the in-computer is restarted the table will be lost.
Basicly the square brackets are used to compare the table data.
I hope this helps!
local Usuarios = {}
while true do
term.clear() term.setCursorPos(1,1) -- clear screen and set cursor at the top
print("1. Login")
print("2. Register")
print("3. Change password")
print("4. Exit")
opc = io.read()
if opc == "1" then
term.clear() term.setCursorPos(1,1)
write("User: ")
user = io.read()
write("Password: ")
pass = read("*")
if Usuarios[user] and Usuarios[user].password == pass then -- check to see if the user exists and then compare the password with the given one
print("Access")
else
print("Denied")
end
sleep(1)
end
if opc == "2" then -- could put elseif here but because im using notepad++ i changed it to an if
term.clear() term.setCursorPos(1,1)
repeat
write("User: ")
user = io.read()
write("Password: ")
pass = read("*")
write("Rewrite password: ")
pass2 = read("*")
if pass ~= pass2 then
print("Passwords don't match")
end
Usuarios[user] = {} -- make a new user table called user e.g if user was 'ham' the table would be called ham
Usuarios[user].password = pass -- gives that user a password variable inside the table
print("Created")
sleep(1)
until pass == pass2
end
end
This way will allow you to have as much users as you want, but when the in-computer is restarted the table will be lost.
Basicly the square brackets are used to compare the table data.
I hope this helps!
#6
Posted 24 February 2013 - 05:59 PM
Woah, thank you very much! =)
And thank you to all too
That helped me
And thank you to all too
That helped me
#7
Posted 24 February 2013 - 10:27 PM
Usuarios[user] = {} -- make a new user table called user e.g if user was 'ham' the table would be called ham
Usuarios[user].password = pass -- gives that user a password variable inside the table
Why make a whole new table, why not just
Usuarios[user] = pass
#8
Posted 24 February 2013 - 11:10 PM
remiX, on 24 February 2013 - 10:27 PM, said:
Usuarios[user] = {} -- make a new user table called user e.g if user was 'ham' the table would be called ham
Usuarios[user].password = pass -- gives that user a password variable inside the table
Why make a whole new table, why not just
Usuarios[user] = pass
But that would replace the user for the password, right?
#9
Posted 25 February 2013 - 12:11 AM
Alerith, on 24 February 2013 - 11:10 PM, said:
remiX, on 24 February 2013 - 10:27 PM, said:
Usuarios[user] = {} -- make a new user table called user e.g if user was 'ham' the table would be called ham
Usuarios[user].password = pass -- gives that user a password variable inside the table
Why make a whole new table, why not just
Usuarios[user] = pass
But that would replace the user for the password, right?
No it would add the username 'user' with the pass 'pass'
LIke this:
tab = {
["derp"] = 'derpspassword'
}
user = 'remiX'
pass = 'pass'
for username, password in pairs( tab ) do
print( username .. ' - ' .. password )
end
print()
tab[user] = pass
for username, password in pairs( tab ) do
print( username .. ' - ' .. password )
end
It would first print derp - derpspassword
and then
derp - derpspassword
remiX - pass
#10
Posted 25 February 2013 - 03:33 AM
I did like that so more information could be kept about the user, if you wanted to add some in later.
But it doesn't really matter.
But it doesn't really matter.
#11
Posted 25 February 2013 - 07:51 AM
to store more info about the user...
userdata={}
userdata["gopher"]={password="derp", accessLevel=5}
userdata["darkrising"]={password="herp", accessLevel=3}
for k,v in pairs(userdata) do
print(k..": pw="..v.derp..", access level="..v.accessLevel)
end
write("name>")
local name=read()
write("pw>")
local pw=read()
--//make sure name exists in the table before accessing members in the user's sub-table
--//otherwise you'll get attempt to index nil errors
if userdata[name] and userdata[name].password==pw then
print("valid login.")
else
print("Incorrect password!!")
#12
Posted 25 February 2013 - 08:23 AM
GopherAtl, on 25 February 2013 - 07:51 AM, said:
to store more info about the user...
userdata={}
userdata["gopher"]={password="derp", accessLevel=5}
userdata["darkrising"]={password="herp", accessLevel=3}
for k,v in pairs(userdata) do
print(k..": pw="..v.derp..", access level="..v.accessLevel)
end
write("name>")
local name=read()
write("pw>")
local pw=read()
--//make sure name exists in the table before accessing members in the user's sub-table
--//otherwise you'll get attempt to index nil errors
if userdata[name] and userdata[name].password==pw then
print("valid login.")
else
print("Incorrect password!!")
Yeah if you want more information about the user then you would make each user have their own table
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











