divorce

function divorce( tab ) --> keys, values

Description

Return an array table containing all the keys in the table, and an array table containing all the values in the table.

Parameters

tab

The table you want to split the keys and values

Return Values

keys

An array table with the keys

values

An array table with the values

Code

--ZFUNC-divorce-v1
local function divorce( tab ) --> keys, values
   local keys = {}
   local values = {}

   for k, v in pairs( tab ) do
      table.insert( keys, k )
      table.insert( values, v )
   end

   return keys, values
end

return divorce

Examples

local t = require( "tapered" )
local divorce = require( "divorce" )
-- util functions
local like = require( "like" )

local keys, values = divorce{ even=2, odd=3, name="pierrot" }

t.ok( like( { "even", "odd", "name" }, keys ) )
t.ok( like( { 2, 3, "pierrot" }, values ) )

t.done()

See also