table
The table type
is an object that can store multiple values.
The table library
is a standard Lua library which provides functions to manipulate tables. In Garry's Mod there are several extra useful functions added to this library.
This category lists the functions available in the table library
.
Methods
Adds all values from source table into the target table. This is most useful for sequential tables, not "dictionary" or "map" tables. See table. Merge if you want to merge 2 tables into one.
See table. insert for a function that adds a single value, and table. Inherit for a function that inherits keys from one table to another.
Changes all keys to sequential integers. This creates a new table object and does not affect the original.
Collapses a table with keyvalue structure
string table.concat( table tbl, string concatenator = "", number startPos = 1, number endPos = #tbl )
Concatenates the contents of a table to a string.
Creates a deep copy and returns that copy.
This function does NOT copy userdata, such as Vectors and Angles!
Empties the target table, and merges all values from the source table into it.
Counts the amount of keys in a table. This should only be used when a table is not numerically and sequentially indexed. For those tables, consider the length (#) operator.
If you only want to test if the table is empty or not, use table. IsEmpty instead as it is a lot faster.
Converts a table that has been sanitised with table. Sanitise back to its original form
We advise against using this. It may be changed or removed in a future update.
Instead, iterate the table using ipairs or increment from the previous index using next. Non-numerically indexed tables are not ordered.
Returns the value positioned after the supplied value in a table. If it isn't found then the first element in the table is returned
We advise against using this. It may be changed or removed in a future update.
Instead, iterate your table with ipairs, storing the previous value and checking for the target. Non-numerically indexed tables are not ordered.
Returns the value positioned before the supplied value in a table. If it isn't found then the last element in the table is returned
Flips key-value pairs of each element within a table, so that each value becomes the key, and each key becomes the value.
Take care when using this function, as a Lua table cannot contain multiple instances of the same key. As such, data loss is possible when using this function on tables with duplicate values.
local test = { test = 1, test2 = 1 }
local f = table. Flip( test )
PrintTable( f )
-- Outputs "1 = test2"
Inserts a value in to the given table even if the table is non-existent
We advise against using this. It may be changed or removed in a future update.
This was deprecated in Lua 5. 1 and removed in 5. 2. You should use pairs instead.
Iterates for each key-value pair in the table, calling the function with the key and value of the pair. If the function returns anything, the loop is broken.
The GLua interpretation of this is table. ForEach.
We advise against using this. It may be changed or removed in a future update.
This was deprecated in Lua 5. 1 and removed in 5. 2. You should use ipairs() instead.
Iterates for each numeric index in the table in order.
This is inherited from the original Lua implementation and is deprecated in Lua as of 5. 1; see here. You should use ipairs() instead.
We advise against using this. It may be changed or removed in a future update.
Instead, expect the first key to be 1.
Non-numerically indexed tables are not ordered and do not have a first key.
We advise against using this. It may be changed or removed in a future update.
Instead, index the table with a key of 1.
Non-numerically indexed tables are not ordered and do not have a first key.
We advise against using this. It may be changed or removed in a future update.
Instead, use the result of the length (#) operator, ensuring it is not zero. Non-numerically indexed tables are not ordered and do not have a last key.
Returns the last key found in the given table
We advise against using this. It may be changed or removed in a future update.
Instead, index the table with the result of the length (#) operator, ensuring it is not zero. Non-numerically indexed tables are not ordered and do not have a last key.
Returns the last value found in the given table
We advise against using this. It may be changed or removed in a future update.
This function was deprecated in Lua 5. 1 and is removed in 5. 2. Use the length (#) operator instead.
Returns the length of the table.
Returns a key of the supplied table with the highest number value.
Checks if a table has a value.
This function is very inefficient for large tables (O(n)) and should probably not be called in things that run each frame. Instead, consider a table structure such as example 2 below. Also see: Tables: Bad HabitsFor optimization, functions that look for a value by sorting the table should never be needed if you work on a table that you built yourself.
Copies any missing data from base to target, and sets the target's BaseClass member to the base table's pointer.
See table. Merge, which overrides existing values and doesn't add a BaseClass member.
See also table. Add, which simply adds values of one table to another.
Sub-tables aren't inherited. The target's table value will take priority.
Inserts a value into a table at the end of the table or at the given position.
This function does not call the __newindex metamethod.
Returns whether or not the given table is empty.
This works on both sequential and non-sequential tables, and is a lot faster for non-sequential tables than table. Count(tbl) == 0.
If you want to check if a table is not empty, use next(tbl) ~= nil, as it is slightly faster.
Returns whether or not the table's keys are sequential
Returns the first key found to be containing the supplied value
Returns a table of keys containing the supplied value
Returns a copy of the input table with all string keys converted to be lowercase recursively
Returns an array of values of given with given key from each table of given table.
See also table. KeysFromValue.
Recursively merges the key-value pairs of the source table with the key-value pairs in the destination table.
See table. Inherit, which doesn't override existing values.
See also table. Add, which simply adds values of one table to another.
This function can cause a stack overflow under certain circumstances.
Moves elements from one part of a table to another part a given table. This is similar to assigning elements from the source table to the destination table in multiple assignments.
Packs a set of items into a table and returns the new table. It is meant as an alternative implementation of table. pack from newer versions of Lua.
Returns a random value from the supplied table.
This function iterates over the given table twice, therefore with sequential tables you should instead use following:
mytable[ math. random( #mytable ) ]
Removes a value from a table and shifts any other values down to fill the gap.
Does nothing if index is less than 1 or greater than #tbl
Removes the first instance of a given value from the specified table with table. remove, then returns the key that the value was found at.
Avoid usage of this function. It does not remove all instances of given value in the table, only the first found, and it does not work with non sequential tables!
Returns a reversed copy of a sequential table. Any non-sequential and non-numeric keyvalue pairs will not be copied.
Converts Vectors, Angles and booleans to be able to be converted to and from key-values via util. TableToKeyValues.
table. DeSanitise performs the opposite transformation.
Sorts a sequential table either ascending or by the given sort function.
This function modifies the table you give to it and internally uses the quick sort algorithm.
Returns a list of keys sorted based on values of those keys.
For normal sorting see table. sort.
Sorts a table by a named member.
Sorts a table in reverse order from table. sort.
This function modifies the table you give to it. Like table. sort, it does not return anything.