Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can i figure out if there is ANYTHING in a table?

Asked by 6 years ago

What i mean by this is for example i have a table and it is

local thetable = {}

and then it adds stuff into it if something is true but then how would i see if there is anything in the table so that i could do another thing?

2 answers

Log in to vote
2
Answered by
Filipalla 504 Moderation Voter
6 years ago
Edited 6 years ago
function isSomethingInTable(table)
    if #table < 1 then
        return false
    end
    return true
end

you would use it like this

local myTable = {}
if not isSomethingInTable(myTable) then
    print("there is nothing in my table")
else
    print("there is something in my table")
end

Wikipages

Tables

Return

If i missunderstod please tell me and ill update my answer

Edit: This works in most cases

Ad
Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

It depends on what kind of table it is. Is it a dictionary, mixed, or just string and numbers or maybe just strings or just numbers?

-- strings and numbers or string or numbers
local tab1 = {123456, "string", '123456'}
print(#tab1) -- > 3
-- dictionary
local tab2 = {
    this_fun = function()
        print('hello')
    end;

    this_string = true
}
print(#tab2) --> 0
print(tab2[this_string]) --> true
print(tab2['this_fun']()) --> hello

for i,v in pairs(tab2) do 
    print(i,v) 
    --> this_fun function #
    --> this_string true
end

-- mixed local tab3 = { this_fun = function() end; this_string = 'string'; 123456, 'string'; {anothertable = true} } print(#tab3) -- > 3 for the number, string, and nested table

Answer this question