marry

function marry( keys, values ) --> tab

Description

Merges keys with values. The both array tables, keys and values, must have the same length, otherwise returns the function nil.

Parameters

keys

An array table where the elements will be interpretet as the keys of the resulting table.

values

An array table where the elements will be interpetet as the values of the resulting table.

Return Values

tab

A merged table.

Code

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

   if #keys ~= #values then
      return nil
   end

   for i = 1, #keys do
      local k = keys[ i ]
      local v = values[ i ]
      tab[ k ] = v
   end

   return tab
end

return marry

Examples

local t = require( "taptest" )
local marry = require( "marry" )
local same = require( "same" )

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

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

t()

See also