157 posts
Posted 27 April 2014 - 07:58 PM
abc = {
{"test", "lol"},
{"abcd", "123"},
{"haba", "abc"}
}
username = "abcd"
function ha(user)
for k, v in pairs(abc) do
if v[1] == user then
return v[1], v[2]
break
end
end
end
u, p = ha(username)
print(u .. " " .. p)
I'm trying to find a matching username and print out the username and password.
Regards
Augustas
Edited on 27 April 2014 - 05:59 PM
1140 posts
Location
Kaunas, Lithuania
Posted 27 April 2014 - 08:22 PM
Next time post
the full error message you get.
Your problem is using
break after
return. After returning (using
return keyword) or breaking out of the loop (using
break keyword) the code after
return or
break isn't executed, plus Lua expects an
end,
else,
until, etc. and doesn't even allow to write any code after those keywords.
It would be easier if you would setup your table using usernames as keys and passwords as values:
local users = {
["testuser"] = "testpassword",
["anotheruser"] = "123"
}
local username = read() --// Ask for a username
local password = read() --// Ask for a password
if users[username] and users[username] == password then --// If the user exists and the password is correct
print("Welcome " .. username .. "!")
else --// If the username or password was incorrect
print("Wrong username or password!")
end
Edited on 27 April 2014 - 06:22 PM
157 posts
Posted 28 April 2014 - 05:49 PM
If I recall correctly, you don't need [" "], you just type in without the square brackets or quotes, it's like a variable, variable being the key and value being the value.
But you can access the key the same way by ["username"] whereas inside the table it's username = "password"
62 posts
Posted 28 April 2014 - 06:03 PM
You need to use brackets if the key has spaces or non-alphanumerical characters or if it starts with a number.