Jump to content

(Help) Tables / Find / Match


Recommended Posts

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

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 by Tails
  • Thanks 1
Link to comment

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

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 👍👍

  • Like 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...