Checking whether a table is empty in Lua

local table_a = {}


    Judgment 1: (wrong method)
    if table_a == {} then
        -- you will find that you can't enter the conditional judgment and always return false, why is this?
        --Reason: {}-> is equivalent to creating a table, but the name of this table is anonymous, you can't see it, we assume it is called table_b --you
        will find that the judgment condition has become a comparison of table_a and table_b memory address. So the conditional judgment always returns false.
        -- Correct implementation method: judge the second  
    end

 

    Judgment 2: (correct method)

            if next(table_a) == nil then
                -- the operation you want to implement
            end

            -- Reason for success: next gets the next content in the table, there is no next content in the empty table, return nil

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326090435&siteId=291194637