Can you find a table in a table?

Hello everyone.

Well, I was trying to find a table in a table to perform the thing. so, I just tried with table with like {Object, String}, but everything returned nil.
Well, I think finding a table in a table doesn’t work, since I put print(table.find({{2,5},{4,7},{22,3}}, {2,5})) and still returned nil.

Is it not working? If then, what is the efficient way to perform the same thing as I said?

3 Likes

Tables work differently than numbers, booleans and strings. Tables are passed by reference, while numbers, booleans and strings are passed by value. This means that two identical tables are not considered same value, while two number 2:s are considered same value. Everytime you use {}, you create a new table. But you can store a table in a variable to reuse the same table.

This should work

local t1, t2, t3 = {2, 5}, {4, 7}, {22, 3}
local mainTable = {t1, t2, t3}
print(table.find(mainTable, t1))
2 Likes

Good explanation @RoBoPoJu. To add to that, I’d only like to append an extention to your post, explaining what you are talking about using dictionaries.

What is the difference between a dictionary and a array? (Arrays are actually similar to dictionaries, but have ascending numbers as indexes!)

local mainTable = {}
table.insert(mainTable, {3, 4}) -- Let's insert a new table.

-- How is this table now stored?
--[[
	mainTable = {
		[1] = {3, 4};
	}
]]

Meaning each time you insert an element into your array, that array is assigned so called index.

What does table.find do?

local mainTable = {"apple", "Bloxy cola", "pizza"}

print(mainTable[1]) --> prints "apple"
print(table.find(mainTable, "pizza")) --> prints "3"; index of pizza

table.find works similar to iterating using a loop:

for index, value in ipairs(mainTable) do
	print(index, value)
end

So when it finds the value we are looking for, it returns either its index, or nil in case there is no occurance of that element in table.

Let’s break down what @RoBoPoJu already explained:

local mainTable = {"apple", {6, 8}}
local found = false

for index, value in pairs(mainTable) do
	if (value == {6, 8}) then
		found = true
	end
end
print(found and "Found it!" or "Nope, no such value found!")
-- OUTPUT:
-->> Nope, no such value found!

Even though contents of those table are the same, tables themselves aren’t (they are unique), so they are rather different.

local mainTable = {
	["t1"] = {2, 5};
	["t2"] = {4, 7};
}

print(mainTable["t1"]) --> {[1] = 2, [2] = 5}

Use indexes.

Even thought we can’t find tables using table.find(), we can always find solid workarounds. Luckly, we aren’t missing anything, otherwise lua would provide some sort of other approach to do it.


EDIT @Mitumitu17YN it's like comparing instances. The following example is a big odd, but you get the point.
-- Instances are present, but not named.
local tableOfInstances = {Instance.new("Part"), Instance.new("Part")}
local found = false

for index, value in ipairs(tableOfInstances) do
	if (value == Instance.new("Part")) then
		found = true
	end
end
print(found and "Found it!" or "Nope, no such value found!")
-- OUTPUT:
-->> Nope, no such value found!

Instance we are comparing table contents to is completely different, and is not a value (string, integer, boolean, float, or event more complex assembled value like vector).

local tableOfInstances = {Instance.new("Part"), Instance.new("Part")}
local found = false

tableOfInstances[1].Parent = workspace --> There is is, but it's unnamed.
14 Likes