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

Is It Possible To Search A Table For A Specific String?

Started by Dejected, 25 September 2013 - 05:30 PM
Dejected #1
Posted 25 September 2013 - 07:30 PM
I'm making a table that holds names and I want to see if the table has a certain name in it
Wojbie #2
Posted 25 September 2013 - 07:36 PM
Is your table indexed numerically?
if then

tab – Your table
look – sting you are looking for



function isin(tab,look)
	for i=1,#tab do
		if tab[i] == look then return i end
	end
	return nil
end


This will return number where look is in table or nil if its not in table

EDIT : Or change "return i" into "return true" and "return nil" into "return false" for true false info.
Dejected #3
Posted 25 September 2013 - 07:45 PM
Is your table indexed numerically?
if then

tab – Your table
look – sting you are looking for



function isin(tab,look)
	for i=1,#tab do
		if tab[i] == look then return i end
	end
	return nil
end


This will return number where look is in table or nil if its not in table

EDIT : Or change "return i" into "return true" and "return nil" into "return false" for true false info.
Worked exactly as I needed it to the first way thanks