Hello, developers! I want to get the total amount of items in a table. This is what I mean:
local responses = {
"Hello!",
"This a test!",
{
":D",
">:("
}
}
--I'm ALSO trying to get the other table inside the "responses" table which contains ":D" and ">:("
print(table.getn(responses)) --> 3
print(#responses) --> 3
Hey, for this you would need to reiterate for values that are a table:
local my_table = {
"Hello",
"Test1",
{
"Hello again",
"Test 2"
}
}
local function getNumberOfItems(tbl)
local items = 0
for index, value in pairs(tbl)do
if typeof(value) == "table" then -- if the value is a table we reiterate to get the number of items in it
items += getNumberOfItems(value) -- you can add 1 if you want to count the table aswell
else
items += 1
end
end
return items
end
local items = getNumberOfItems(my_table)
print(items) -- 4