This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TechnicalCoding's profile picture

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

Started by TechnicalCoding, 28 July 2016 - 11:01 PM
TechnicalCoding #1
Posted 29 July 2016 - 01: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 on 28 July 2016 - 11:02 PM
valithor #2
Posted 29 July 2016 - 01: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 on 28 July 2016 - 11:14 PM
TechnicalCoding #3
Posted 29 July 2016 - 01:10 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)
How would I check the username table which is in the last layer of the SQL table?
valithor #4
Posted 29 July 2016 - 01:13 AM
How would I check the username table which is in the last layer of the SQL table?


check(sql["oss"]["Users"]["username"],username_input)
TechnicalCoding #5
Posted 29 July 2016 - 01:15 AM
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?
valithor #6
Posted 29 July 2016 - 01:21 AM
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.
TechnicalCoding #7
Posted 29 July 2016 - 01:22 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)

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

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
valithor #8
Posted 29 July 2016 - 01:23 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)

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

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"
TechnicalCoding #9
Posted 29 July 2016 - 01: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 on 28 July 2016 - 11:49 PM
valithor #10
Posted 29 July 2016 - 02:34 PM
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 on 29 July 2016 - 12:36 PM