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

[Question] Equal to any ?

Started by Mackan90096, 13 May 2013 - 01:14 AM
Mackan90096 #1
Posted 13 May 2013 - 03:14 AM
Hey! I'm making a "cheat code" thing and I'm wondering if you can have like a equal to any thing?check
So for example:


local codes = {code1 = "1337h4xx0rz", code2 = "yolo" }


Can you check if the input is any of those codes in a table ?

(I don't want to use lots of elseifs)
theoriginalbit #2
Posted 13 May 2013 - 03:24 AM
Something like this can be used to see if its a value in the table

local function strValueInTable(str, tbl)
  for k,v in pairs(tbl) do
	if str == v then
	  return true
  end
  return false
end

Something like this can be used to see if it is a key in a table

local function strKeyInTable(str, tbl)
  return tbl[str] ~= nil
end

EDIT:
Then when you call the function just give it the string to look for as the first param, and the table as the second param….

you could also make one that does this

local function strKeyOrValueInTable(str, tbl)
  for k,v in pairs(tbl) do
	if str == v or str == k then
	  return true
  end
  return false
end
Edited on 13 May 2013 - 01:26 AM