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( "taptest" )
local divorce = require( "divorce" )
local like = require( "like" )

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

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

t()

See also