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 the contents from one table into another. The target table will be modified. See also table.insert, table.Inherit and table.Merge.
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.
table table.CollapseKeyValue( table input )
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.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. This is inherited from the original Lua implementation and is deprecated in Lua as of 5.1; see here. You should use pairs instead. 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.For 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 to use than table.Count(tbl) == 0.
boolean table.IsSequential( table tab )
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
table table.LowerKeyNames( table tbl )
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.Merge( table destination, table source )
Merges the contents of the second table with the content in the first one. The destination table will be modified. See table.Inherit, which doesn't override existing values. See also table.Add, which simply adds values of one table to another. This function will cause a stack overflow under certain circumstances.
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. table.DeSanitise does the opposite
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.
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

Page Links


Special Pages


Wikis

?

Render Time: 75ms

DB GetPage 47
Generate Html 2
SaveChanges (1) 11
Render Body 0
Render Sidebar 12