Table.find() doesn't seem to work

Hi
In my code I have an if statement that is meant to check if chunkInfo (table) is inside of chunksToRender (table).
I set up a 3 prints, first one shows chunksToRender table, second one shows chunkInfo table and third one shows the output of table.find()

print(chunksToRender)
print(chunkInfo)
print(table.find(chunksToRender, chunkInfo))

And here is the output:
image
Does anyone know why does this happen and what can I do to fix it?

May you send the full code, so I can see how it works?

Thanks for replying, I don’t see why that matters. I just tested few thing in a fresh studio file.

local tableA = {{"a","b"}, {"c", "d"}}
local B = {"a","b"}

print(table.find(tableA, B))

output: nil

local tableA = {{"a","b"}, {"c", "d"}}
local tableB = {{"a","b"}, {"c", "d"}}

print(tableA == tableB)

output: false

Does that mean you can’t compare tables? And what would be a work around for it?

I men’t I need to see whats “chunkInfo”, “chunksToRender”

It should be something like this,

local tbl = {'a','b','c','d'}

local letter_a = "a"

if table.find(tbl,letter_a) then
     print(table.find(tbl,letter_a))
else
  print('a was not found')
end

in lua tables are handled by reference and when you compare two tables, you actually compare their references (not their contents). So a table can only be equal to itself.

In this case you are creating two tables (with the “{}” operator) and they will never be equal because they have different references.

local tableA = {{"a","b"}, {"c", "d"}}
local tableB = tableA

print(tableA == tableB)

In this case you only create one table that will be equal to itself.

You can use a table comparison algorithm like this:

function CompareArray(t1,t2)
    if #t1~=#t2 then return false end
    for i=1,#t1 do 
		if t1[i]~=t2[i] then return false end
	end
    return true
end

and use it in a loop to find the position of your chunkInfo table.

2 Likes