Garry's Mod Wiki

Revision Difference

Beginner_Tutorial_Tables#517291

<cat>Dev.Lua</cat>⤶ <title>Beginner Tutorial Tables</title>⤶ ⤶ # What Are Tables?⤶ ⤶ Tables are basically variables that hold 2 different things, Keys and Values⤶ ⤶ This is an extremely basic explaination but there is so much more to them than what is here⤶ ⤶ ## Keys⤶ ⤶ Keys are identifiers for values, generally keys represent the order of things aka⤶ ⤶ [1]⤶ ⤶ [2]⤶ ⤶ [3]⤶ ⤶ [4]⤶ ⤶ [5]⤶ ⤶ ⤶ but keys can be strings if needed⤶ ⤶ ["String1"]⤶ ⤶ ["String2"]⤶ ⤶ ["String3"]⤶ ⤶ ⤶ ⤶ ## Values⤶ ⤶ Values are any form of data type, even other tables!⤶ ⤶ ⤶ ⤶ ## Table Example⤶ ⤶ ```lua⤶ table1 = {⤶ [1] = "Value",⤶ [2] = "Value"⤶ }⤶ ⤶ ⤶ for k,v in pairs(table1) do -- for key and value in pairs, aka if both of them exist in the table table1 do the following⤶ print(k .. " " .. v)⤶ end⤶ ⤶ returns⤶ ⤶ 1 Value⤶ 2 Value⤶ ⤶ ⤶ ⤶ ⤶ ```⤶ ⤶ ⤶ ## Usage of table. functions⤶ ⤶ ⤶ ```lua⤶ ⤶ table1 = {⤶ "Hello",⤶ "My",⤶ "Name",⤶ "Is",⤶ "Orion"⤶ }⤶ ⤶ table2 = {⤶ "The",⤶ "Coolest",⤶ "Person"⤶ }⤶ ⤶ table.Merge(table1, table2)⤶ ⤶ print(table1)⤶ ⤶ The⤶ Coolest⤶ Person⤶ Is⤶ Orion⤶ ⤶ ⤶ ```⤶