Garry's Mod Wiki

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

table table.Add( table target, table source )
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.
table table.ClearKeys( table table, boolean saveKeys = false )
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.
table table.Copy( table originalTable )
Creates a deep copy and returns that copy. This function does NOT copy userdata, such as Vectors and Angles!
table.CopyFromTo( table source, table target )
Empties the target table, and merges all values from the source table into it.
number table.Count( table tbl )
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.
table table.DeSanitise( table tbl )
Converts a table that has been sanitised with table. Sanitise back to its original form
table.Empty( table tbl )
Removes all values from a table.
any table.FindNext( table tbl, any value )
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
any table.FindPrev( table tbl, any value )
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
table table.Flip( table input )
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"
table table.ForceInsert( table tab = {}, any value )
Inserts a value in to the given table even if the table is non-existent
table.foreach( table tbl, function callback )
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.
table.foreachi( table table, function func )
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.
any table.GetFirstKey( table tab )
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.
any table.GetFirstValue( table tab )
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.
table table.GetKeys( table tabl )
Returns all keys of a table.
any table.GetLastKey( table tab )
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
any table.GetLastValue( table tab )
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
number table.getn( table tbl )
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.
any table.GetWinningKey( table inputTable )
Returns a key of the supplied table with the highest number value.
boolean table.HasValue( table tbl, any 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.
table table.Inherit( table target, table base )
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.
number table.insert( table tbl, number position, any value )
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.
boolean table.IsEmpty( table tab )
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
any table.KeyFromValue( table tab, any value )
Returns the first key found to be containing the supplied value
table table.KeysFromValue( table tab, any 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
number table.maxn( table tbl )
Returns the highest numerical key.
table table.MemberValuesFromKey( table inputTable, any keyName )
Returns an array of values of given with given key from each table of given table. See also table. KeysFromValue.
table table.Merge( table destination, table source, boolean forceOverride = false )
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.
table table.move( table sourceTbl, number from, number to, number dest, table destTbl = sourceTbl )
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.
table, number table.Pack( vararg items )
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.
any, any table.Random( table haystack )
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 ) ]
any table.remove( table tbl, number index = #tbl )
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
any table.RemoveByValue( table tbl, any val )
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!
table table.Reverse( table tbl )
Returns a reversed copy of a sequential table. Any non-sequential and non-numeric keyvalue pairs will not be copied.
table table.Sanitise( table tab )
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.
table.Shuffle( table target )
Performs an inline Fisher-Yates shuffle on the table in O(n) time
table.sort( table tbl, function sorter )
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.
table table.SortByKey( table tab, boolean descending = false )
Returns a list of keys sorted based on values of those keys. For normal sorting see table. sort.
table.SortByMember( table tab, any memberKey, boolean ascending = false )
Sorts a table by a named member.
table.SortDesc( table tbl )
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.
string table.ToString( table tbl, string displayName, boolean niceFormatting )
Converts a table into a string