Garry's Mod Wiki

table.ClearKeys

table table.ClearKeys( table table, boolean saveKeys = false )

Description

Changes all keys to sequential integers. This creates a new table object and does not affect the original.

Arguments

1 table table
The original table to modify.
2 boolean saveKeys = false
Save the keys within each member table. This will insert a new field __key into each value, and should not be used if the table contains non-table values.

Returns

1 table
Table with integer keys.

Example

Changes all the table's keys to integer values

local Table = {One = "A", Two = "B", Three = "C"} local Table2 = table.ClearKeys(Table) PrintTable(Table2)
Output: 1 = A

2 = C

3 = B

Example

Clears a table of its keys, and preserves the old key names within each member.

local tbl = { FirstMember = { Name = "John Smith", Age = 25 }, SecondMember = { Name = "Jane Doe", Age = 42 }, ThirdMember = { Name = "Joe Bloggs", Age = 39 } } print( "===== Before =====" ) PrintTable( tbl ) local tbl2 = table.ClearKeys( tbl, true ) print( "===== After =====" ) PrintTable( tbl2 )
Output:
===== Before ===== FirstMember: Name = John Smith Age = 25 SecondMember: Name = Jane Doe Age = 42 ThirdMember: Name = Joe Bloggs Age = 39 ===== After ===== 1: Age = 25 Name = John Smith __key = FirstMember 2: Age = 39 Name = Joe Bloggs __key = ThirdMember 3: Age = 42 Name = Jane Doe __key = SecondMember

Page Links


Special Pages


Wikis

?

Render Time: 43ms

Session 0
DB GetPage 3
Generate Html 17
SaveChanges 8
Render Body 0
Render Sidebar 12