kong.table

Utilities for Lua tables.

kong.table.new([narr[, nrec]])

Returns a table with a pre-allocated number of slots in its array and hash parts.

Parameters

  • narr (number, optional): Specifies the number of slots to pre-allocate in the array part.
  • nrec (number, optional): Specifies the number of slots to pre-allocate in the hash part.

Returns

  • table: The newly created table.

Usage

  1. local tab = kong.table.new(4, 4)

kong.table.clear(tab)

Clears all array and hash parts entries from a table.

Parameters

  • tab (table): The table to be cleared.

Returns

  • Nothing.

Usage

  1. local tab = {
  2. "hello",
  3. foo = "bar"
  4. }
  5. kong.table.clear(tab)
  6. kong.log(tab[1]) -- nil
  7. kong.log(tab.foo) -- nil

kong.table.merge([t1[, t2]])

Merges the contents of two tables together, producing a new one. The entries of both tables are copied non-recursively to the new one. If both tables have the same key, the second one takes precedence. If only one table is given, it returns a copy.

Parameters

  • t1 (table, optional): The first table.
  • t2 (table, optional): The second table.

Returns

  • table: The (new) merged table.

Usage

  1. local t1 = {1, 2, 3, foo = "f"}
  2. local t2 = {4, 5, bar = "b"}
  3. local t3 = kong.table.merge(t1, t2) -- {4, 5, 3, foo = "f", bar = "b"}