Lua Tables — Queries

If you have imported the module as

require 'lulu.table'

Then you have access to the following query methods:

Method Brief Description
table.size(tbl) Returns the number of top-level items in tbl.
This works correctly for any Lua table.
table.is_table(tbl) Returns true if a table is a Lua table.
table.is_array(tbl) Returns true if a table is a Lua array.
table.is_array_of(tbl, e_type) Returns true iftbl is a Lua array where all the elements are of the given type e_type.
table.is_array_of_one_type(tbl) Returns true iftbl is a Lua array where all the elements are of the same type.
table.is_array_of_numbers(tbl) Returns true iftbl is a Lua array where all the elements are numbers.
table.is_array_of_strings(tbl) Returns true iftbl is a Lua array where all the elements are strings.
table.metadata(tbl) Returns a metadata table about tbl.
A Lua array is a Lua table where the keys are consecutive integers starting at 1 with no gaps.

Metadata

The table.metadata(tbl) function returns a table of metadata about a table tbl.

The returned table, md, has a sub-table md[t] for each sub-table t encountered in tbl, including tbl itself.

The md[t] sub-table has the following fields:

Field Description
md[t].array Boolean that is true if t is a Lua array.
md[t].size Number of elements in t.
md[t].subs Number of proper sub-tables in t.
md[t].refs Number of references to t. Greater than one if t is shared.

This metadata helps understand the structure of a table. The metadata function can handle tables that contain circular references.

See Also

table.keys
table.values
{table.value_counts}
{table.key_set}
{table.value_set}

Back to top