kong.table

kong.table

Utilities for Lua tables

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

Returns a table with 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)

Back to top

kong.table.clear(tab)

Clears a table from all of its array and hash parts entries.

Parameters

  • tab (table): the table which will 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

Back to top

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"}

Back to top