Library table

Lua's (not glua's) table library

Functions

concat( array, separator, startIndex, endIndex ) Returns the concatenation of all values in the given array as a string.
foreach( table, callback ) Executes the given function for every key-value in the given table.
foreachi( array, callback ) Executes the given function for every array value in the given table.
getn( table ) Returns the length of the array part of the given table.
insert( table, index, value ) Inserts the given value into the given table.
maxn( table ) Returns the maximum numeric key in the given table.
pack( ... ) Returns a new table whose array part is populated with the given argumnents.
remove( table, key ) Removes the value held at the given table key, shifting later array values down.
sort( table, comparator ) Sorts the given table in-place, optionally using the given comparator function.
unpack( table, index, endIndex ) Unpacks the values in the given table into individual return values.


Functions

table.concat( array, separator, startIndex, endIndex )
Returns the concatenation of all values in the given array as a string.

Environment:

Shared

Parameters:

  • (table) array: The array to concatenate.
  • (string?) separator: An optional separator to insert between each array value (default = "").
  • (number?) startIndex: An optional start index within the array to concatenate values from (inclusive, default = 1).
  • (number?) endIndex: An optional end index within the array to stop concatenating values at (inclusive, default = #array).
table.foreach( table, callback )
Executes the given function for every key-value in the given table.

Deprecated: Use the pairs() iterator instead.

Environment:

Shared

Parameters:

  • (table) table: The table to iterate.
  • (function) callback: The function that will be called for each key-value pair, passed the key and value.
table.foreachi( array, callback )
Executes the given function for every array value in the given table.

Deprecated: Use the ipairs() iterator or a numeric for-loop instead.

Environment:

Shared

Parameters:

  • (table) array: The array to iterate.
  • (function) callback: The function that will be called for each array value, passed the index and value.
table.getn( table )
Returns the length of the array part of the given table.

Deprecated: Use the '#' operator instead.

Environment:

Shared

Parameters:

  • (table) table: The table to get the length of the array part from.
table.insert( table, index, value )
Inserts the given value into the given table.

Environment:

Shared

Parameters:

  • (table) table: Table to insert the given value into.
  • (number?) index: An optional index to insert the value into (default = #table + 1).
  • (Any) value: The value to insert.

Usage:

    local Array = {} 
    -- Insert a new value at the end of the array. 
    table.insert( Array, "example" ) 
    -- Insert a new value at the start of the array. 
    table.insert( Array, 1, "this is an" )
table.maxn( table )
Returns the maximum numeric key in the given table. Unlike '#', this does not require a sequence of values.

Environment:

Shared

Parameters:

  • (table) table: The table to get the highest numeric key from.
table.pack( ... )
Returns a new table whose array part is populated with the given argumnents. An additional 'n' key is also added which specifies the number of arguments the table was constructed from (including nils).

Environment:

Shared

Parameters:

  • ...: The values to pack into a new table's array part.
table.remove( table, key )
Removes the value held at the given table key, shifting later array values down.

Environment:

Shared

Parameters:

  • (table) table: The table to remove the given key from.
  • (number) key: The key to remove.

Return value:

    (Any) The value that was removed, or nil if no such key existed.
table.sort( table, comparator )
Sorts the given table in-place, optionally using the given comparator function.

Environment:

Shared

Parameters:

  • (table) table: The table to sort.
  • (function?) comparator: An optional comparator function to determine the sorting order. Called with two values, and should return true if the first value is before the second, or false otherwise.
table.unpack( table, index, endIndex )
Unpacks the values in the given table into individual return values. This is the same as the global unpack() function.

Environment:

Shared

Parameters:

  • (table) table: Table to unpack into a vararg.
  • (number?) index: The index to start unpacking from (default = 1).
  • (number?) endIndex: The index to finish unpacking at (default = #table).

Return value:

    (Any...) A value per entry in the table unpacked.

Valid XHTML 1.0!