countlen

function countlen( tab ) --> len

Description

Counts the number of top level entries in a table.

Parameters

tab

Table where the length should be counted.

Return Values

len

Length of the table.

Code

--ZFUNC-countlen-v1
local function countlen( tab ) --> len
   local len = 0
   for _ in pairs( tab ) do
      len = len+1
   end
   return len
end

return countlen

Examples

local t = require( "taptest" )
local countlen = require( "countlen" )

tab = {}
t( countlen( tab ), 0 )

tab = { "a", "b", "c" }
t( countlen( tab ), 3 )

tab = {
   [ "a" ] = { 1, 2, 3 },
   [ "b" ] = "xyz",
   [ "c" ] = nil,
   [ "d" ] = { 1 }
}
t( countlen( tab ), 3 )

t()