What is the best way to do this

ok let’s say I want to get every single value from a table, and I don’t know how many tables within tables there are since the table would be random

what is the best way to do this?
I have something that might work since I almost have it working, but I get out of my last day of school in like 45 minutes so I’m just asking for ideas on how to do it

Example

{5, {10, {3, 2}}, 6}
I would want to be able to mess with each value with a for loop which would be 5, 10, 3, 2, and 6

remember I wouldn’t know this would have a table within a table within a table, that’s the hard part
maybe they would all be numbers or maybe they would all be tables with 5 layers!

thanks, doryu

added an example so it would be easier to understand

Check out this post:

Then just add the values to a new table.

Go through recursion

local function do_something_with_table_value(t)
   for _, value in pairs(t) do
      if (type(value) ~= 'table') then
         -- do something with the value
      else
         do_something_with_table_value(value) -- calling the function again passing the value (which is a table) as an argument
      end
   end
end

I had this exact same format except I used a loop instead of recursion

don’t know why I didn’t think of this, ik there are other answers but I’ll make this the solution since it’s the first one, guess that’s what you get when you make stuff at 1 am in the morning

1 Like