clear

function clear( tab ) --> slimtab

Description

Removes all elements from the table tab.

Parameters

tab

The table you want to clear.

Return Values

slimtab

The same table where all elements are removed.

Code

--ZFUNC-clear-v1
local function clear( tab ) --> slimtab
   for k in pairs( tab ) do
      tab[ k ] = nil
   end
   return tab
end

return clear

Examples

local t = require( "taptest" )
local clear = require( "clear" )
local isempty = require( "isempty" )

-- array table
tab = { "a", "b", "c", "d", "e", "f" }
slimtab = clear( tab )
t( isempty( tab ), true )
t( isempty( slimtab ), true )
t( tab, slimtab )

-- dict table
tab = { [ "first" ] = 1, [ "second" ] = 2, [ "third" ] = 3  }
slimtab = clear( tab )
t( isempty( tab ), true )
t( isempty( slimtab ), true )
t( tab, slimtab )

t()

Inspired by