Here's an example of what I've tried to do:
Numbers = {100, 102} local ChosenNumber = 102 for i,v in pairs(Numbers)do if v ~= Numbers[i] then print("I am not "..ChosenNumber) --It'll print it even though 102 is in the table but since it starts from the beginning, it finds 100 and since its not 102 it'll print "I am not 102" end end
So how can I make it check the whole table then decide if ChosenNumber is inside of the table or not?
Can someone help me get past this problem?
You can use the table.find()
function to look for a specified value inside a table. The function will return the index at which the value first occurs at.
The function has 3 arguments: Table, Value, and "Init".
The table argument is the table where you want to find the specified value.
The value argument is the value you're looking for in the table.
The third "Init" argument is used to define at which point the function will start looking for the value, every index lower than init will be ignored.
Here's an example of the function in use:
local table1 = {"foo","bar",1,2,3} print(table.find(table1,"bar"))
Output:
2
The function returns 2 since the bar component of table1 occurs at the table's second index.
Here's an example where we use the third argument, init.
local table1 = {"foo","bar",1,2,3,"bar"} print(table.find(table1,"bar",3))
Output:
6
Now that the init value is set in place, you can see that the second index is not the output, but rather the 6th index. This is because we now defined a point to start, which is after the second index, and thus it is ignored.