Garry's Mod Wiki

table.Flip

  table table.Flip( table input )

Description

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"

Arguments

1 table input
The table to flip items of.

Returns

1 table
The flipped table.

Example

local productIngredients = { "Cocoa Mass", "Cocoa Butter", "Vanilla", "Cocoa Solids: 70% min" } local flipped = table.Flip( productIngredients ) PrintTable( flipped )
Output:
Cocoa Butter = 2 Cocoa Mass = 1 Cocoa Solids: 70% min = 4 Vanilla = 3