Check if string is in Table without giving...
TechnicalCoding 29 Jul 2016
I am working on a in game SQL Server for CC, but I need to be able to check if a string is in a table without using conditions like "root"=true, "user1"=true
This is my current table I want to check,
how may i do that? please help!
Edited by TechnicalCoding, 29 July 2016 - 12:02 AM.
This is my current table I want to check,
local sql = { oss = { Users = { id = { 0 }, username = { "root" }, password = { "" }, first_name = { "Root" }, last_name = { "User" }, active = { 1 } } } }I've also got the read function for username which is named: username_input
If sql[oss][users][username][username_input] (And String exists code)
how may i do that? please help!
Edited by TechnicalCoding, 29 July 2016 - 12:02 AM.
valithor 29 Jul 2016
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
Edited by valithor, 29 July 2016 - 12:14 AM.
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor) then
Edited by valithor, 29 July 2016 - 12:14 AM.
TechnicalCoding 29 Jul 2016
valithor, on 29 July 2016 - 12:07 AM, said:
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor)
valithor 29 Jul 2016
TechnicalCoding 29 Jul 2016
Will they be en Speech marks even though in the table teir not?
valithor 29 Jul 2016
TechnicalCoding, on 29 July 2016 - 12:15 AM, said:
When referencing a table with notation like: tbl[key] the key must be whatever is in the table. If you were to put: sql[oss], the interpreter would see oss and try to find some variable named oss. Since there is none it would look in the sql table for a nil key, which isn't what you wanted. So, when referencing the table you must put quotation/speech marks.
TechnicalCoding 29 Jul 2016
valithor, on 29 July 2016 - 12:07 AM, said:
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor)
I have tried both with speech marks and without, Everything it returns on if statements is this:
startup:107: attempt to index ? (nil value)
line 107
if check(sql["oss"]["users"]["username"], username_input) == true then
valithor, on 29 July 2016 - 12:07 AM, said:
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor)
I have tried both with speech marks and without, Everything it returns on if statements is this:
startup:107: attempt to index ? (nil value)
line 107
if check(sql["oss"]["users"]["username"], username_input) == true then
valithor 29 Jul 2016
TechnicalCoding, on 29 July 2016 - 12:22 AM, said:
valithor, on 29 July 2016 - 12:07 AM, said:
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor)
I have tried both with speech marks and without, Everything it returns on if statements is this:
startup:107: attempt to index ? (nil value)
line 107
if check(sql["oss"]["users"]["username"], username_input) == true then
valithor, on 29 July 2016 - 12:07 AM, said:
You would need to manually loop through the table and see if the string is in there by comparing each entry to the string you are looking for.
example function:
Example usage:
example function:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true end end return false end
Example usage:
if check(tableToCheck,StringToCheckFor)
I have tried both with speech marks and without, Everything it returns on if statements is this:
startup:107: attempt to index ? (nil value)
line 107
if check(sql["oss"]["users"]["username"], username_input) == true then
Lua is case sensitive. It should be "Users" not "users"
TechnicalCoding 29 Jul 2016
How may i get the string position in the username table and then use it to get the string of first_name in the same position?
Edited by TechnicalCoding, 29 July 2016 - 12:49 AM.
Edited by TechnicalCoding, 29 July 2016 - 12:49 AM.
valithor 29 Jul 2016
TechnicalCoding, on 29 July 2016 - 12:48 AM, said:
How may i get the string position in the username table and then use it to get the string of first_name in the same position?
The k value in the loop in the check function will be equal to the index of the match. You could modify the check function to:
function check(tbl,str) for k,v in ipairs(tbl) do if v == str then return true,k end end return false end
And then you would need to change your if statement to look like this:
local bool, index = check(sql["oss"]["users"]["username"], username_input) if bool then --# the string exists in the tbl, and index is the position in the table end
Edited by valithor, 29 July 2016 - 01:36 PM.