How to find the value in a table in a table in a table.. Yeah you get the idea

I’ve got this table right, but it has tables in that table, some of them ALSO have other tables inside them. But I seem to have forgotten how you find a value in a table, regardless of how many tables it’s in

(i.e., like FindFirstChild([Instance], true) it will find what your looking for no matter how many times its grouped or foldered. That’s what I’m trying to achieve.)

Example of what I mean:


Solved:

Solved by @TheRealANDRO with @12345koip initial help.

5 Likes

which format use the table?

Most of the tables own the following format:

  • table {object.table}
  • table[object] = table
  • table {table}
2 Likes

I constructed a little example that might help better at understanding.

Like I said, I’m looking for a value you in a table that contains tables, and those tables might also contain tables (i.e., like parts in groups of groups, etc), in a loop. It’s a function that acts like FindFirstChild([Instance], true) but for tables. I used to know how to get a value deep within tables of tables, but I forgot.

2 Likes

Can you do just something like

for table
  for another table
    for one more table?
1 Like

The table, has tables, and in SOME of those tables are also more tables.

3 Likes

I don’t understand how there isn’t a :GetDescendants() for tables. Using a for loop on a table that has tables, only gets those tables but if those tables have also got tables inside them, you can reach them without making another for loop, it’s just messy and ridiculous.

2 Likes

Use recursion. For each table, the function calls itself. This function has a for loop where it checks each index of the table, if found the desired value, it returns the value, closing the recursion.

2 Likes

use a recursive function to search the tables
@TheRealANDRO beat me to it while i was writing a code example :c

anyway here’s a recursive linear search for a table, tested! It returns the table the value was in, as well as it’s key within that table (if it was found).

local function recursiveLinearSearch<T, V>(searchTarget: T, value: V): ({[any]: any}?, any?)
	--search the initial table first
	for k, v in next, searchTarget, nil do
		--check the value
		if (v == value) then
			return searchTarget, k --return the table and the index
			
		elseif (type(v) == "table") then --wasn't the value, but it was a table
			local tbl, index = recursiveLinearSearch(v, value) --search that table too
			
			if (index) then --if it was found in that table, return it
				return tbl, index
			end
		end
	end
	
	--not found
	return nil, nil
end
3 Likes

Believe it or not I found a solution which is actually quite similar to yours, although not as fancy and complicated. It’s just a test, so I will obviously improve it, but in case anyone wants to know how to do this here’s a picture of the code:
image
Basically just loops through a table to find what you’re looking for, if it can’t find it, it will keep trying. The issue with this is it will keep trying and never stop until it finds what your looking for or break the script. May get out of hand. But as I said, it’s just a basic test.

Oh also thank you for your help as well, I can’t believe we both had a similar idea.

3 Likes

You can just call it like any other structure.

local nestedTable = {
	outer = {
		middle = {
			inner = "desiredValue"
		}
	}
}

local value = nestedTable.outer.middle.inner
print(value)
1 Like

Sorry, I didn’t reply. Yep, I eventually came up with that like @12345koip did as well.

1 Like

That’s not what I was looking for, but it’s ok it’s solved now, thank you though. :slight_smile:

2 Likes

Well… As it turns out, me solution didn’t work in my script, but worked in the command bar. So I tried your code, and I got this instead, not sure what’s going on.


image

1 Like

At this point I’m lost, I have no idea why it’s not working. Any help would be appreciated please.

1 Like

Weird, what is “Windows”? Can you send it?

1 Like

It’s a table of many tables, i just put the a, b, and c ones at the bottom and commented the the rest out to see if it where the tables causing the issue, but apparently not.

1 Like

Here I shortened it and also tested it just in case, if for some reason length might of had something to do with it, I don’t know…


Result:
image

1 Like

oh, it’s because you’re looking for the key, not the value. Typically, search algorithms will return the key of the element, and the element is being looked for. So, if you put in true, it would return table c and also findThisOneHere.

So, hence to stick with typical design, my code snippet looks for the value and returns the key.

1 Like

I also tried formatting the table how I actually have it, but makes no difference, still get the same result.
image

1 Like

If you want to look for the key, just change the check if v == value to if k == value. You might want to rename some variables after that, though.

1 Like