How to get the Children of a Table inside of a Table

Hello!
I would like to know if there is a way to get the children of a table thats inside of another table…
Below there is my table and a few lines of code that uses the table, which is where im trying to get children of a table in a table

my table
local BingoTypes = {
	Bingo1 = {"1", "2", "3", "4", "5"},
	Bingo2 = {"6", "7", "8", "9", "10"},
	Bingo3 = {"11", "12", "13/FREE", "14", "15"},
	Bingo4 = {"16", "17", "18", "19", "20"},
	Bingo5 = {"21", "22", "23", "24", "25"},
	Bingo6 = {"1", "6", "11", "16", "21"},
	Bingo7 = {"2", "7", "12", "17", "22"},
	Bingo8 = {"3", "8", "13/FREE", "18", "23"},
	Bingo9 = {"4", "9", "14", "19", "24"},
	Bingo10 = {"5", "10", "15", "20", "25"},
}
the code that uses this table
	for i, v in pairs(BingoNum) do
		if BingoNum[i] == --[[the get children inside of a table of a table that im looking for]] then
			print("test")
		end
	end
end)

--BingoNum is a different table dont worry about it

you could do

for i,v in pairs(BingoNum[Bingo1]) do
1 Like

Meant all the tables (bingo1,2,3,etc). Should of put that in the post, my bad

for i,v in pairs(BingoTypes) do
for t, r in pairs(v) do
print(i,r)
 --[[
Bingo1 = 1
Bingo1 = 2
...
Bingo2 = 6
Bingo2 =7
etc..
]] 
end
end

or

for i,v in pairs(BingoTypes) do
print(i.." : "..table.concat(v,',')) -- Bingo1 : 1,2,3,4,5 etc..
end
1 Like

Maybe use recursion, something (untested!) similar to this perhaps.

local function dealWithValue(idx,val)
  print("index at ", idx, " has the value ", val);
end
local function iterateTableLevel(theTable)

  if(theTable==nil or typeof(theTable)~='table') then
  return end;

  for i, v in pairs(theTable) do
    if(typeof(v)=='table' then
      -- recurse
      iterateTableLevel(v);
    else
      dealWithValue(i,v);
    end
  end
end

Call it with iterateTableLevel(BingoTypes);

2 Likes

Could you explain this a little bit more? Not really sure how I could intergrate this with my current script

-- for the code that uses the table
for key, bingo in pairs(BingoNum) do
	print("START INNER:", key)
	for i, str in pairs(bingo) do
		print("\t", i, str)
	end
end

--> START INNER: Bingo1
-->     1 1
-->     2 2
-->     ...

Iterating over BingoNum gives back the BingoX keys and the array of strings to the right of that key. Iterating over that array gives back the array index and whatever is stored at that index.

You might not encounter Bingo1 first in your collection of tables because it’s a dictionary. If you want that behavior, try making it a 2D array.

local BingoNum = {
	-- the '[number] =' part is optional
	[1] = { "1", "2", "3", "4", "5" },
	[2] = { "6", "7", "8", "9", "10" },
	-- ...
}
1 Like

Dont think this would work since bingo is an instance not a table

yeah, i get this error from it

Ah, sorry, I mistook BingoTypes for BingoNum. I should’ve read it better.

Does that mean BingoTypes is in a script somewhere, and you want to access it in a different script?

What even is BingoNum?

1 Like

This is what I was able to create based on your post:

local BingoTypes = {
	Bingo1 = {"1", "2", "3", "4", "5"}, -- 4th
	Bingo2 = {"6", "7", "8", "9", "10"}, -- 2nd
	Bingo3 = {"11", "12", "13/FREE", "14", "15"}, -- 5th
	Bingo4 = {"16", "17", "18", "19", "20"}, -- 6th
	Bingo5 = {"21", "22", "23", "24", "25"}, -- 9th
	Bingo6 = {"1", "6", "11", "16", "21"}, -- 10th
	Bingo7 = {"2", "7", "12", "17", "22"}, -- 8th
	Bingo8 = {"3", "8", "13/FREE", "18", "23"}, -- 7th
	Bingo9 = {"4", "9", "14", "19", "24"}, -- 1st
	Bingo10 = {"5", "10", "15", "20", "25"}, -- 3rd
}

local function check(list)
	if list == nil or typeof(list) ~= "table" then
		return nil
	end
	
	local newTable = {}
	
	local function iterate(input)
		table.foreach(input, function(k, v) 
			if typeof(v) == "table" then
				print(k)
				iterate(v)
			else
				table.insert(newTable, v)
			end
		end)
		--[[ another way to loop
		for k, v in pairs(input) do
			if typeof(v) == "table" then
				iterate(v)
			else
				table.insert(newTable, v)
			end
		end
		]]
	end
	
	iterate(list)
	
	return newTable
end

print(table.unpack(check(BingoTypes)))

If necessary, we can make Bingo1 first and Bingo10 last, but I don’t know if that’s what you need.

1 Like

BingoNum is just an array that stores the numbers that were called out and the player has those numbers
Just realised how poorly made my post is, the tables and code I put are in the same script just spaced out with the BingoNum code

Do you need Bingo1 first and Bingo10 last?

1 Like

You said the things in BingoNum are instances. Are they a StringValue? TextLabel?

1 Like

Uh, not really sure, dont think it would make any difference

Did you find a solution, or is there something more that needs to be fixed with the code?

1 Like

doesn’t this solve your issue?

1 Like

Just woke up and made a new post about this without realising that i already made this one… apologies, this doesnt really fit what im trying to make

I think I explained my issue better in the new post since this one is horribly explained

1 Like