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

Finding a string in a table

Started by KittenTheEpic, 30 January 2014 - 10:25 AM
KittenTheEpic #1
Posted 30 January 2014 - 11:25 AM
I want to make a smart login server. If you logged in, it will add you to a table of authenticated ID's. But how do I make the server scan the table for the ID to see if it is authenticated or not?
CometWolf #2
Posted 30 January 2014 - 12:10 PM
Im guessing you just index the table numerically, so in that case you can just use a for loop

tId = {
  1 = "derp",
  2 = "derpy"
}
function check(id)
  for i=1,#tId do --the # returns the number of numerical keyes from 1 to the first nil, so here it will return 2
	if tId[i] == id then
	  return true
	end
end
if check"derp" then
  print"id found!"
end
Edited on 30 January 2014 - 11:11 AM
Bomb Bloke #3
Posted 30 January 2014 - 04:51 PM
On the other hand, if you were to index the table using the IDs themselves, you'd just perform a simple existence check.

tId = {
  ["derp"] = true,
  ["derpy"] = true
}

function check(id)
  return tId[id]
end

if check("derp") then
  print("id found!")
end

More entries can be added to the table later by using code along the lines of:

tId[user] = true