←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Check if string is in Table without giving...

TechnicalCoding's Photo 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,
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.
Quote

valithor's Photo 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:

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

TechnicalCoding's Photo TechnicalCoding 29 Jul 2016

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?
Quote

valithor's Photo valithor 29 Jul 2016

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)
Quote

TechnicalCoding's Photo TechnicalCoding 29 Jul 2016

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?
Quote

valithor's Photo valithor 29 Jul 2016

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

TechnicalCoding's Photo TechnicalCoding 29 Jul 2016

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
Quote

valithor's Photo valithor 29 Jul 2016

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"
Quote

TechnicalCoding's Photo 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.
Quote

valithor's Photo valithor 29 Jul 2016

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