table.ClearKeys

From Garry's Mod
Jump to: navigation, search
 table.ClearKeys( )

Contents

Description

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

Arguments

table table

The original table to modify.

Arguments

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

table

Table with integer keys.

Examples

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

Examples

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
Personal tools
Navigation