Jump to content




Check if string is in Table without giving the string a condition in table


9 replies to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

Posted 29 July 2016 - 12:01 AM

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


#2 valithor

  • Members
  • 1,050 posts

Posted 29 July 2016 - 12:07 AM

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:

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.


#3 TechnicalCoding

  • Members
  • 35 posts

Posted 29 July 2016 - 12:10 AM

View Postvalithor, 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:

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)
How would I check the username table which is in the last layer of the SQL table?

#4 valithor

  • Members
  • 1,050 posts

Posted 29 July 2016 - 12:13 AM

View PostTechnicalCoding, on 29 July 2016 - 12:10 AM, said:

How would I check the username table which is in the last layer of the SQL table?

check(sql["oss"]["Users"]["username"],username_input)


#5 TechnicalCoding

  • Members
  • 35 posts

Posted 29 July 2016 - 12:15 AM

View Postvalithor, on 29 July 2016 - 12:13 AM, said:

View PostTechnicalCoding, on 29 July 2016 - 12:10 AM, said:

How would I check the username table which is in the last layer of the SQL table?

check(sql["oss"]["Users"]["username"],username_input)
Will they be en Speech marks even though in the table teir not?

#6 valithor

  • Members
  • 1,050 posts

Posted 29 July 2016 - 12:21 AM

View PostTechnicalCoding, on 29 July 2016 - 12:15 AM, said:

View Postvalithor, on 29 July 2016 - 12:13 AM, said:

View PostTechnicalCoding, on 29 July 2016 - 12:10 AM, said:

How would I check the username table which is in the last layer of the SQL table?

check(sql["oss"]["Users"]["username"],username_input)
Will they be en Speech marks even though in the table teir not?

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.

#7 TechnicalCoding

  • Members
  • 35 posts

Posted 29 July 2016 - 12:22 AM

View Postvalithor, 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:

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

View Postvalithor, 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:

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


#8 valithor

  • Members
  • 1,050 posts

Posted 29 July 2016 - 12:23 AM

View PostTechnicalCoding, on 29 July 2016 - 12:22 AM, said:

View Postvalithor, 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:

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

View Postvalithor, 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:

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"

#9 TechnicalCoding

  • Members
  • 35 posts

Posted 29 July 2016 - 12:48 AM

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.


#10 valithor

  • Members
  • 1,050 posts

Posted 29 July 2016 - 01:34 PM

View PostTechnicalCoding, 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.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users