# table

This is an expansion to the existing Lua Table library.


# Functions

# table.concat(t: table, sep: string, start?: number, end?: number)

table.concat(t: table, sep: string, start?: number, end?: number): string
  • Returns the (numeric) entries in the table t, concatenated together with "sep" as the separator, starting at index 'start' and ending at index 'end'.
  • The entries are returned as a single string variable.
  • Contrast this to the "unpack" function which returns a table as individual variables.

# table.foreach(t: table, f: function(key: any, value: any))

table.foreach(t: table, f: function(key: any, value: any)): any?
  • Executes f for each element in table t.
  • If f returns a non-nil value the loop is broken, and this value is returned as the result from table.foreach.
  • Effectively this could be used to find an element inside a table matching a certain condition.

# table.foreachi(t: table, f: function(key: any, value: any))

table.foreachi(t: table, f: function(key: any, value: any)): any?
  • Similar to table.foreach, except that only numeric keys in the range 1 to n are processed.

# table.getn(t: table): number

table.getn(t: table): number
  • Returns the size of a numerically-keyed table.
  • A table's size (which is only relevant for "numeric" indexed tables) is one less than the first integer index with a nil value.
  • If the table has "holes" in it - that is, numeric keys with gaps in the sequence - then the table size is not guaranteed to be the the last gap.
  • Lua does a binary search to try to find a gap, it does not necessarily find the first or last one.

# table.insert(t: table, pos: number, value: any)

table.insert(t: table, pos: number, value: any)
  • Inserts the value at (optional) position 'pos', renumbering existing elements if necessary to make room. Thus the new element becomes the one with index 'pos'.
  • If called with 2 arguments, the value is inserted at n+1, that is, the end of the table.

# table.maxn(t: table): number

table.maxn(t: table): number
  • Returns the highest numeric key in the table, by examining each entry in the entire table.
  • This will necessarily be slower than doing table.getn, but would be needed if the keys have gaps in the sequence.
  • f the table does not have gaps in the keys, it is faster to use table.getn(t: table): number

# table.remove(t: table, pos?: number): any

table.remove(t: table, pos?: number): any
  • Removes the element at position 'pos' from the table, returning the value of the removed element.
  • If 'pos' is omitted it defaults to the end of the table (n), thus removing the last element.

# table.sort(t: table, f?: function)

table.sort(t: table, f?: function)
  • Sorts the table using the supplied function f as the comparison function for each element.
  • Function f should return true if the first element is < the second element. If the function omitted it defaults to the operator <.
  • Sorting is not stable, that is, the sequence of equal keys is not necessarily preserved.