Simon54 Posted July 30 Share Posted July 30 Hi, I can't seem to figure this out even after going over the Tables TUT. I have a number value that I need to match with a value inside a different table. All is good, I'm just not sure how to loop through the other table (numberTable) to see if a value is present that does match data[1] I tried something like this: for key, value in ipairs( numberTable ) do if data[1] == table.find( numberTable ) then outputChatBox("match found") end end Link to comment
Tails Posted July 31 Share Posted July 31 (edited) Could you share the structure of your tables and what you're trying to match exactly? table.find is not a function in Lua, you have to either run another loop or if the data table has the same structure you can do this: for i, v in ipairs(numbersTable) do if data[i] and data[i] == v then print('match found', v) end end or maybe with a loop: for i, numbersValue in ipairs(numbersTable) do for j, dataValue in ipairs(data) do if data[j] == numbersValue then print('match found', v) break end end end It all depends on how your tables are structured and how you want to match it, it's kind of hard to guess what the best approach is. Edited July 31 by Tails 1 Link to comment
Tails Posted July 31 Share Posted July 31 In my second example I used data[j] but you can just put dataValue there. either way is fine. Link to comment
Simon54 Posted July 31 Author Share Posted July 31 This is basically the idea or structure ========================================================== -- setting data theFistTable = {} -- {2, 4, 6, 8, 10} table.insert( theFistTable, 10 ) table.insert( theFistTable, 8 ) table.insert( theFistTable, 6 ) table.insert( theFistTable, 4 ) table.insert( theFistTable, 2 ) theSecondTable = {} -- {2, 4, 6, 8, 10} table.insert( theSecondTable, 2 ) table.insert( theSecondTable, 4 ) table.insert( theSecondTable, 6 ) table.insert( theSecondTable, 8 ) table.insert( theSecondTable, 10 ) setElementData( thePlayerElement, "data", theSecondTable ) ========================================================== -- matching data local data = getElementData( thePlayerElement, "data" ) for key, value in ipairs( theFirstTable ) do if data[1] == table.find( theFirstTable ) then outputChatBox("match found") end end Link to comment
Simon54 Posted July 31 Author Share Posted July 31 This method works, it's able to match: for i, numbersValue in ipairs(numbersTable) do for j, dataValue in ipairs(data) do if data[j] == numbersValue then print('match found: '..numbersValue, v) break end end end Thanks Tails 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now