If I made a list of letters, how would I check to see if another letter is in the table?
local fruitTable = {"apple", "banana", "pear", "orange"}
I have fruit called Peach, how would I check to see if it is in fruitTable
?
There are a few ways.
The best way is to make a dictionary;
local fruitTable = { apple = true, banana = "Test", pear = { 1, 2, 3 }, orange = false } print(fruitTable["apple"]); -- true print(fruitTable["pineapple"]); -- nil print(fruitTable["orange"]); -- false print(fruitTable["banana"]); -- "Test" print(fruitTable["pear"][1]]); -- 1 print(fruitTable["pear"][2]]); -- 2 print(fruitTable["pear"][3]); -- 3
or to use a function:
function tableFind(Table, toFind) local found = false; if typeof(Table) == "table" then for i,v in pairs(Table) do if(v == toFind) then found = true end; end return found; else return "First parameter has to be a table."; end end