Jump to content




How to compare data of a table


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

#1 Alerith

  • Members
  • 36 posts

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

#2 AnDwHaT5

  • Members
  • 244 posts

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.

#3 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 24 February 2013 - 01:38 PM

View PostAlerith, on 24 February 2013 - 01:31 PM, said:

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
Pastebin your existing code.

#4 Alerith

  • Members
  • 36 posts

Posted 24 February 2013 - 01:45 PM

Thanks for the response, here is my code:

http://pastebin.com/haZYEm0J

#5 darkrising

  • Members
  • 234 posts
  • LocationScotland

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.
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 Alerith

  • Members
  • 36 posts

Posted 24 February 2013 - 05:59 PM

Woah, thank you very much! =)
And thank you to all too :D

That helped me

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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 Alerith

  • Members
  • 36 posts

Posted 24 February 2013 - 11:10 PM

View PostremiX, 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 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 25 February 2013 - 12:11 AM

View PostAlerith, on 24 February 2013 - 11:10 PM, said:

View PostremiX, 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 darkrising

  • Members
  • 234 posts
  • LocationScotland

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.

#11 GopherAtl

  • Members
  • 888 posts

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 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 25 February 2013 - 08:23 AM

View PostGopherAtl, 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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users