| Lua table functions |
|---|
| Lua table functions These are the functions in the "table" 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. If you are planning to treat your tables as vectors (that is, with numeric keys and a size) then we recommend that you do not allow gaps in the sequence. If you want to know if a table is empty, or not, you can test: table.concat (t, sep, start, end) 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. Start defaults to 1 and end the table size. Sep defaults to an empty string. table.foreach (t, f) Executes f for each element in table t. Function f is called with the arguments (key, value). 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. Warning the use of table.foreach is deprecated. This means it may not be available in future versions of Lua. You are recommended to rewrite such uses by using the 'pairs' function. table.foreachi (t, f) Similar to table.foreach, except that only numeric keys in the range 1 to n are processed. In this example the entry for "name = 'Nick'" was not returned because it did not have a numeric key. Warning the use of table.foreachi is deprecated. This means it may not be available in future versions of Lua. You are recommended to rewrite such uses by using the 'ipairs' function. table.getn (t) Returns the size of the table using the rules described at the start of this page. Note that the length here is 4 and not 5, because the non-numeric entry (name = "Nick") is not considered to be part of the table's "length". You can also use #t to find the length of a table. table.insert (t, pos, value) 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. The Lua authors recommend using the idiom "#t + 1" to insert to the end of a table nowadays. For example: table.maxn (t) 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. table.remove (t, pos) 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. The Lua authors recommend using this method for removing from the end of a table nowadays: table.setn (t, n) This has been removed from Lua 5.1. Attempting to call it will raise an error. The length of a table can not be set, it is implied by the highest numeric key, providing there are no gaps in the sequence of numeric keys. table.sort (t, f) 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. Sorting is really only relevant for numerically keyed tables. If you want to sort the keys for other types of tables you need to make a copy of the keys and sort that, like this: Here is an example of a custom sort function. This is needed here because we are sorting tables, which do not have a natural "less than" operator: We can see from the results that the two tables were sorted into "str" order. An alternative approach to supplying a comparison function for the sort would be to set up a metatable for the individual table items (not the container table) which specifies a __lt (less than) operator. Here is an example: In this case I have made a metatable "mt" which is then applied to each table item. It compares the "wis" field in this case. With this in place the sort can be called without a helper function. Of course, for speed purposes you would do this once (perhaps when creating the individual table entries) rather than every time you wanted to sort it. See Also ... Topics
Lua base functions
Lua coroutine functions
Lua debug functions
Lua io functions
Lua math functions
Lua os functions
Lua package functions
Lua script extensions
Lua string functions(Help topic: general=lua_tables)
Documentation contents page |