I’m not sure I understand, in my actual code, the tables contain values (in this case like [“findThisOneHere”]) that are equal to an instance, I want to find that value and configure the instance.
It’s already like that though, I’ll try the other one?
yup, there’s been a little mix up.
In data structures like dictionaries, we typically call the thing on the left the key, and the thing on the right the value. They are key-value pairs.
So, when I said key, I meant what you just called the value.
yes, I said to change it to that, but also you might want to do some variable renaming to make your code more understandable afterwards, if that does work.
I know
I tried it, it worked but, I I tried adding stuff to the one I’m looking for, and I can’t seem to access them. Any clue?
Result:
I will do eventually when I can fix this.
Oh, I just read through the replies and someone already suggested a recursive search. I already wrote it up so I’ll share anyways .
local NestedTable = {
1,
{2, 3, {4, 5, "target"}},
{6, {7, 8, {9}}},
10
}
local function RecursiveTableFind(Table, Value)
for Key, Val in Table do
if Val == Value then
return true -- Okay, already found it so end.
end
if type(Val) == "table" then -- Oh, another table. Let's call the same function on it.
local Found = RecursiveTableFind(Val, Value)
if Found then
return true
end
end
end
return false
end
print("Is the value in the table? Answer: " .. tostring(RecursiveTableFind(NestedTable, "target")))
-- Prints: Is the value in the table? Answer: true
That’s because the code snippet returns the table and the key, not the value held with the key. It was only a slight adjustment to the already existing value searcher, but you can get the value if you index a
with b
.
Or, you can use this one, which will return the table the key was in and the value associated with the key:
local function recursiveLinearSearch<T, K>(searchTarget: T, key: K): ({[any]: any}?, any?)
for k, v in next, searchTarget, nil do
if (k == key) then
return searchTarget, searchTarget[k]
elseif (type(v) == "table") then
local tbl, v = recursiveLinearSearch(v, key)
if (tbl and k) then
return tbl, v
end
end
end
return nil, nil
end
This seems to do the trick, thank you so much, I will apply this to my actual code and get back to you Thanks!
Well for some reason, it’s erroing and printing out “stack overflow”, I’m really confused.
hmm…
I tried it on your table you sent in your snippet and it seemed to work:
Is there a deeper table structure? A stack overflow usually happens when a function calls itself too many times and goes too deep.
I just found out the issue – Remember how I said that what I’m looking for is referencing an instance, the “stack overflow” problem is because the function is also looping through the connected instance, and it’s properties, methods, tables, etc, etc…
So the table structure was to deep, causing the function to call itself too many times and eventually cause a stack overflow. What’s the bit you need checking, though? You could ignore all the other bits with a continue
if it’s a table you don’t want to be checked.
Yes… quite deep, the connected instance also has tables, but, I just want to access the instance alone, then as you’ve seen above I can do (like in this example) findThisOneHere.foo
or findThisOneHere["foo"]
.
so it was trying to check tables which do not have a reference to an instance? If there’s no other tables containing an instance nested within a table like that, you can use a continue
if it doesn’t have an Instance
key (presuming the key referencing all different instances is the same, Instance
as an example)
That’s the whole idea of this though, checking the entire tables, searching for what I’m looking for. Thank of it as a very tidy and optimised workspace, you want to find an object deep within groups and folders, you know?
If my above statement is wrong, you’ll have to split it up to search deeper so it doesn’t recurse too deep. Say, it goes a certain depth, and then returns that to the original statement, and the statement then calls the function again from the top depth, etc.
I think there’s only 2 that are equal to nil, but I’m not looking for them, so it doesn’t matter, and as shown in an old example, I have an example of something being nil, the function doesn’t seem to bother by it.
When you say “tables which do not have a reference to an instance” I assume you mean this:
local t = {
foo = {
etc...
}
thisOneIsNotRefrencingAnythingAtTheMomment = nil
}
I’m honestly not too sure what you mean by this, at this point, I give up, the fact that there isn’t a kind of :GetDescendants() for tables is annoying, ill just have to rewrite 10k lines of code to adapt to less organised tables… This is ridiculous.
Thank you so much for you help though, it means a lot, thank you.
setting a key to nil
is pointless in Luau, as that signals to the garbage collector that it should be cleaned up, almost like a delete
statement in other languages.
By tables with nested instance references, I meant this:
local a = { --this references an instance
Instance = --[[some instance]],
b = { --no instance reference, but nested table does
c = {
Instance = --[[some instance]]
}
}
}
shame you gave up
if you give me a little more info about the table structure and your script overall, i could help more
I know, It’s hard to explain why I have some of them set to nil, but to put it simply, they aren’t yet assigned to something, they are just there to remind me. I could comment them out.
I know, and that is how its constructed, but some tables with (in this case) have a d table, maybe am e too, etc etc.
I’m sorry, it’s just too much of a faf. When I said I would instead rewrite 10k lines of code, I kind of over exaduated that, but at least 2k. I rather rewrite 2k lines of code than waste days trying to recreate the :GetDesandents() method but for tables.
I’m not sure what else there is to give, its a table with nested tables, some with not many tables, some with a lot, simple really.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.