Determine if a table contains a specific element

Hi,

I’m trying to see if a table contains a specific element.  The following doesn’t work and I can’t figure out why.

It prints “horse” 3 times,  so I know it’s counting the number of elements correctly and the value I want to test is being passed, but I get a “nope” rather than a “yes”

local theList = {"dog","cat","horse"} local function checkTheList(theList, val) for k,v in pairs(theList) do print(val) if k == val then return true end end return false end if checkTheList(theList, "horse") then print("yes") else print("nope") end

Unless you’re using named items the index returned as ‘k’ will be the numeric index. Try printing both ‘k’ and ‘v’ with ‘val’.

In short, you want to compare ‘v’ and ‘val’.

local theList = {"dog","cat","horse"} local function checkTheList(theList, val) for k,v in pairs(theList) do if v == val then return true end end end if checkTheList(theList, "horse") then print("yes") else print("nope") end

It returns nil if not finding

if the primary purpose of your list is to determine existence, then use the value also as key, fe:

local theList = { dog="dog", cat="cat", horse="horse" }

then existence is simply:

local function checkTheList(theList,val) return theList[val]~=nil end

@horacebury, @yvandotet  thanks for making it click. Seeing both k and v printed to the console really helped.

v == val has it doing what I need.

Thanks @davebollinger  the purpose is to keep a list of unlocked characters, if the character exists in the list, then show the character.

Values won’t be deleted from the list but new ones will be added as the player unlocks more characters.  

Do I not need code to tell the function to step through the table to see if a value exists?

for k,v in pairs(theList) do if v == val then return true end

not needed - in fact, if your only purpose is to check existence, then just store a boolean (ie, need not store a string value)

local theList = { dog=true, cat=true, horse=true } local function checkTheList(list,key) return list[key]==true end print(checkTheList(theList,"dog")) print(checkTheList(theList,"aardvark")) -- later acquire aardvark also theList["aardvark"] = true print(checkTheList(theList,"aardvark"))

thanks!

Unless you’re using named items the index returned as ‘k’ will be the numeric index. Try printing both ‘k’ and ‘v’ with ‘val’.

In short, you want to compare ‘v’ and ‘val’.

local theList = {"dog","cat","horse"} local function checkTheList(theList, val) for k,v in pairs(theList) do if v == val then return true end end end if checkTheList(theList, "horse") then print("yes") else print("nope") end

It returns nil if not finding

if the primary purpose of your list is to determine existence, then use the value also as key, fe:

local theList = { dog="dog", cat="cat", horse="horse" }

then existence is simply:

local function checkTheList(theList,val) return theList[val]~=nil end

@horacebury, @yvandotet  thanks for making it click. Seeing both k and v printed to the console really helped.

v == val has it doing what I need.

Thanks @davebollinger  the purpose is to keep a list of unlocked characters, if the character exists in the list, then show the character.

Values won’t be deleted from the list but new ones will be added as the player unlocks more characters.  

Do I not need code to tell the function to step through the table to see if a value exists?

for k,v in pairs(theList) do if v == val then return true end

not needed - in fact, if your only purpose is to check existence, then just store a boolean (ie, need not store a string value)

local theList = { dog=true, cat=true, horse=true } local function checkTheList(list,key) return list[key]==true end print(checkTheList(theList,"dog")) print(checkTheList(theList,"aardvark")) -- later acquire aardvark also theList["aardvark"] = true print(checkTheList(theList,"aardvark"))

thanks!