'I have a set of keys in a table but I don't know how to check it
This is what I got so far, It just prints out "incorrect code" with table.concat, but I get an error with table.find
local keys = {"KEY_!@#%$!@#$%^&*(AF^FJ#GSG#FSV#%345R#3553d", "KEY_FJJN@UJFISFSFSF#&%W*CN#CUIWUUC#*W(*C$357275C"}
local key = "KEY_!@#%$!@#$%^&*(AF^FJ#GSG#FSV#%345R#3553d"
if key == table.find(keys) then
print("Success")
else
print("Incorrect Key")
end
Solution 1:[1]
table.concat is for joining an array-like table of strings and numbers into one string with optional string separator (default: ''), and start and end indices.
table.concat { 'h', 3, 'l', 'l', 0 } -- creates the string `h3ll0`.
There is no table.find in Lua's Standard Table Library. Does your implementation or environment provide one? If it does, I would expect the function would at least need to know what you are looking for. Something like
table.find(t, value)
because as is you are not passing any information about key to that function.
Otherwise, you will need to write your own function that searches a table for a given value.
See: Check if array contains specific value.
Solution 2:[2]
you do not have a set of keys in the common programming sense of the word, and that is the solution to your question.
a set is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set. -Wikipedia
you can convert your keys table into a set, where you simply index the set with your key to find if the key is present in the set.
local keys = {"KEY_!@#%$!@#$%^&*(AF^FJ#GSG#FSV#%345R#3553d", "KEY_FJJN@UJFISFSFSF#&%W*CN#CUIWUUC#*W(*C$357275C"}
local keyset = {}
for _, v in ipairs(keys) do
keyset[v] = true
end
local key = "KEY_!@#%$!@#$%^&*(AF^FJ#GSG#FSV#%345R#3553d"
if keyset[key] then
print("Success")
else
print("Incorrect Key")
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oka |
| Solution 2 |
