Jump to content




[LUA][Question] Tables


14 replies to this topic

#1 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 October 2012 - 03:04 PM

I haven't really worked with tables and I'm wondering if something like this would work.

Passwords = {[1] = "pass1", [2] = "derp", [3] = "lol"}

while true do
  term.clear()
  term.setCursorPos(1,1)
  print("Enter password")
  write("  > ")
  checkPassword = read("*")
  if checkPassword = Passwords[1] then
    print("You entered 'pass1'")
    sleep(3)
  elseif checkPassword = Passwords[2] then
    print("You entered 'derp'")
    sleep(3)
  elseif checkPassword = Passwords[3] then
    print("You entered 'lol'")
    sleep(3)
  else
    print("Invalid password: "..checkPassword)
  end
end

If it does, is there an easier way to do it so you don't have to have an elseif for each password?

#2 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 23 October 2012 - 03:39 PM

pretty much like this but here we also check for usernames
local users = {"User1"}
local passw = {"Pass1"}
local function usercheck(u)
  for x=1,#users do  -- #users is the lenght of the user table
    if users[x] == u then
	  return true, x
    end
  end
return false
end
print("Username:  ")
local user = read()
local check, id = usercheck(user)
if check then
  term.clear()
  term.setCursorPos(1,1)
  print("Password: ")
  local pass = read("*")
  if passw[id] == pass then
    print("Login Sucessfull")
  else
    print("Wrong Password")
  end
else
  print("Wrong Username")
end


#3 ChunLing

  • Members
  • 2,027 posts

Posted 23 October 2012 - 04:22 PM

Yes, you just reverse the keys and values in your table:
Passwords = {pass1=1,derp=2,lol=3} 

Then you use a single if then end:
checkPassword = read("*")
if Passwords[checkPassword] then print("You entered "..checkPassword) else print("Invalid password: "..checkPassword) end

Edited by ChunLing, 23 October 2012 - 04:24 PM.


#4 Doyle3694

  • Members
  • 815 posts

Posted 23 October 2012 - 04:29 PM

the for loop function is very much prefered, doing it like ChunLing is in my opinion the "glitchy" way. for loops are neater in my opinion

#5 ChunLing

  • Members
  • 2,027 posts

Posted 23 October 2012 - 04:41 PM

What's glitchy about it? It's super fast and I've used it intensively without ever having any kind of failure.

#6 Doyle3694

  • Members
  • 815 posts

Posted 23 October 2012 - 04:43 PM

because you are using variable names as a variable. not much else to say

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,462 posts

Posted 23 October 2012 - 04:48 PM

View PostDoyle3694, on 23 October 2012 - 04:43 PM, said:

because you are using variable names as a variable. not much else to say

There's nothing technically incorrect or glitchy about ChunLing's method. It has the potential to lead to code that is confusing to people not used to using tables that way, but there's nothing wrong with it. Though, I would use true for their value rather than an index if I were just using it to validate entries without trying to match them to anything, like his example.

#8 Doyle3694

  • Members
  • 815 posts

Posted 23 October 2012 - 04:51 PM

No I know there is nothing incorrect or glitchy with it. but it feels very strange.

#9 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 October 2012 - 05:25 PM

Cool ^_^/>

Other than login systems and menu's, what else can you use tables for in lua?

#10 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 23 October 2012 - 05:55 PM

pretty much anything that stores an unknown length of data or you can do weird encryption tools with it like this:
encrypter: http://pastebin.com/inEL4D1r
decrypter: http://pastebin.com/f0Cywah1

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 October 2012 - 09:13 PM

View PostKolpa, on 23 October 2012 - 05:55 PM, said:

pretty much anything that stores an unknown length of data or you can do weird encryption tools with it like this:
encrypter: http://pastebin.com/inEL4D1r
decrypter: http://pastebin.com/f0Cywah1

Does this work 100% for a login system? ^_^/>

#12 ChunLing

  • Members
  • 2,027 posts

Posted 23 October 2012 - 10:18 PM

You can still use the value under the key, which I assume you'd want if you have three different passwords rather than one.

I guess that you could use the shift encryption on a login...don't know why you would, but you could.

#13 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 October 2012 - 11:02 PM

View PostChunLing, on 23 October 2012 - 10:18 PM, said:

You can still use the value under the key, which I assume you'd want if you have three different passwords rather than one.

I guess that you could use the shift encryption on a login...don't know why you would, but you could.

So the host of the server can't check the passwords xD

#14 ChunLing

  • Members
  • 2,027 posts

Posted 23 October 2012 - 11:58 PM

Aha...well that particular encryption scheme is pretty weak, you'd want something stronger for persistent data. But yes, you could make it so that the user input an encryption key along with their password, and only the encrypted match would be stored on the server, not the key or original password.

You could also make it so that the first few characters of the password were automatically converted into an encryption key for the password, for ease of use.

Edited by ChunLing, 24 October 2012 - 12:01 AM.


#15 Kolpa

  • New Members
  • 260 posts
  • LocationGermany

Posted 24 October 2012 - 12:42 PM

yeah don't use ceaser shift for security its really weak

you could use this function in combination with the http api to create md5 hashes
local function hash(str)
return http.get("http://im-to-poor-to-buy-an-domain.host22.com/md5.php?string="..str).readLine()
end

and save passwords/usernames in that encryption

also using tables is pretty bad since its slow and buggy it would be better to use byte shifting for caesar shift





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users