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( "tapered" )
local countlen = require( "countlen" )

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

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

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

t.done()