Garry's Mod Wiki

History for Coding - Tables

Revision Difference

Beginner_Tutorial_Tables#525316

<cat>Dev.GettingStarted</cat> <title>Coding - 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 Tables are variables that allow you to store multiple values within them, like a container. Tables provide you with the ability to map **values** to specific **keys**, like how a dictionary maps definitions (the values) to specific words (the keys). Going off of the dictionary example, one can look up any word in any dictionary and get the definition of it in return, in the same way that one can look up a key in a table and retrieve the value stored *at*, or *on*, that key. Keep in mind that this is an extremely basic explanation, and there really is so much more to tables than what is described here. ## Keys Keys are identifiers for values, generally keys represent the order of things aka [1] [2] [3] [4] [5] As mentioned above, tables in Lua contain keys and values; keys are identifiers for values, and are known as the "index" of any entry. Entries in a table might also be called "key/value pairs." One could use keys in multiple ways. First, keys can be numeric and/or represent the order of things, i.e.: ```lua [1] [2] [3] [4] [5] -- and so on... ``` but keys can be strings if needed ["String1"] ["String2"] ["String3"] Keys can also be strings, such as: ```lua ["String1"] ["AnotherOne"] ["EvenMore"] -- and so on... ``` When used to represent the order of things, keys let a table function more like `list`, or even `array`, objects in other languages - the keys are numeric, and sequential (in order). When strings (or other types) are used as keys, tables can be used more like a `dictionary` or hash map object. The best part of all this is that tables in Lua are able to use both of these kinds of "modes" at the same time! It's not just numbers or strings that can be used as keys, however. According to the official Lua Users' Wiki, keys can be any type of variable, except for `nil`. More often than not, however, they are usually defined as either numbers or strings. There can only be one value for every key in a table - keys must be unique. The only way to store more than one value on a specific key is to use a new table as the value. (This is still technically only one value, though - the table counts as a single value, but can have other stuff stored inside of it. This table-inside-a-table scenario is called a "nested" table.) ## Values Values are any form of data type, even other tables! Values can be any data type - even other tables! ## Table Example ## Table creation example ```lua -- Here, we are creating an empty table using the "table constructor" ( {} ) emptyTable = {} -- We can create a table with predefined values: table1 = { [1] = "Value", [2] = "Value" [1] = "First", [2] = "Second", } -- (Remember to put a comma after every 'key = value' line.) 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 -- We can also have Lua fill in numeric "order" values automatically: table1 = { "First", "Second" } -- table1 will then contain [1] = First, and [2] = Second. returns 1 Value 2 Value -- We can define string keys on our table in two different ways: -- First, the shorthand way: table1.SomeStringKey = "Hello!" -- or, the long way: table1["SomeStringKey"] = "Hello!" -- To verbally describe both of these methods, we could say that: -- "table1 AT SomeStringKey EQUALS Hello!" ``` ``` ## Looping over a table There are a few ways to loop over all of the key/value pairs in a table. The method you use will depend on how the keys are arranged. If Lua filled in your numeric "order" keys automatically, like in the last example, you can use a for loop to loop through the table just fine. If there are any keys that were NOT defined as numeric "order" keys, i.e. string keys, OR if you defined a numeric key "out of order," you will have to use `pairs()` to loop over the table. `pairs` will loop over all of the values in the table, including the numeric/order keys AND the string (or other type) keys; in other languages, this is kind of like a `for each` loop. ```lua -- Let's redefine our table, using the automatic numeric/order key method: table1 = { "First", "Second", "Third" } -- Now, I will take the same table we just made, and define a string key on the table. table1.StringKey = "Fourth?" -- Before putting the StringKey in the table, we could have just used a forloop, since -- all of the keys were automatically defined as ordered numbers. -- However, since there is now a StringKey in the table, we need to use pairs() to loop -- over the table, since a forloop won't "get" the entry for the StringKey. ## Usage of table. functions -- Let's use a forloop to loop over the numeric values. -- The variable 'i' will act as our key, and we can get the corresponding value -- by referencing 'table1[i]': for i=1,#table1 do print( i .. " == " .. table1[i] ) end -- This should print: -- 1 == First -- 2 == Second -- 3 == Third ```lua Fruits = { "Apple", "Orange", "Banana", "Pineapple" -- ...now let's see what that looks like with pairs. for k,v in pairs( table1 ) do -- Inside the pairs loop! -- Let's print each key in the table, and its corresponding value. print( k .. " == " .. v ) end -- This should print: -- 1 == First -- 2 == Second -- 3 == Third -- StringKey == Fourth? ``` ## Usage of the table library The <page>table</page> 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. These are some examples of functions defined in the `table` library. ### table.Merge ```lua -- We define a table full of fruits, Fruits = { "Apple", "Orange", "Banana", "Pineapple", } Veggies = { "Pepper", "Tomato" -- And a table full of veggies. Veggies = { "Pepper", "Tomato" } table.Merge(Fruits, Veggies) print(table.concat(Fruits," ")) Pepper Tomato Banana Pineapple -- We will use the table.Merge function to merge values in 'Veggies' into the 'Fruits' table. table.Merge( Fruits, Veggies ) -- args: to, from -- When this runs: -- the "Pepper" value from the Veggies table will replace the "Apple" value in Fruits (key '1'), and: -- the "Tomato" value from the Veggies table will replace the "Orange" value in Fruits (key '2'). -- Let's print all of the new values in 'Fruits': print( table.concat(Fruits," ") ) -- This will output: -- Pepper Tomato Banana Pineapple ``` ### table.Add ```lua Fruits = { "Apple", "Orange" -- Again, we have our Fruits and Veggies tables. Fruits = { "Apple", "Orange" } Veggies = { "Pepper", "Tomato" "Pepper", "Tomato" } table.Add(Fruits, Veggies) print(table.concat(Fruits," ")) Apple Orange Pepper Tomato -- Add all the values in Veggies to the end of the Fruits table: table.Add(Fruits, Veggies) -- args: to, from print( table.concat(Fruits," ") ) -- This will output: -- Apple Orange Pepper Tomato ``` ## Usage of "pairs" ```lua Names = {"John","Mary","Mark","Den"} for key,value in pairs(Names) do print(key.." - "..value) ### Other table functions You can refer to the library page for the `table` library to discover all of the `table` library's functions. ## Other functions Some other functions are also useful for tables, including, but not limited to: ### ipairs We went over the use of `pairs` in the *Looping over a table* section. `pairs` has a sister function called `ipairs` that does the exact same thing `pairs` does (loops over all the k/v pairs in a table), but for the numeric keys only. ```lua Names = { [2] = "John", [1] = "Mary", [4] = "Mark", [3] = "Den" } -- Use ipairs to loop through the key/value pairs with numeric keys, and print them out: for k,v in ipairs( Names ) do print( k.. " == " .. v ) end 1 - John 2 - Mary 3 - Mark 4 - Den -- This should output: -- 1 == Mary -- 2 == John -- 3 == Den -- 4 == Mark ``` ## Usage of "ipairs" ```lua Names = {[2] = "John", [1] = "Mary",[4] = "Mark",[3] = "Den"} for key,value in ipairs(Names) do print(key.." - "..value) ### RandomPairs `RandomPairs` loops through a table, but in a random order. ```lua Names = { "John", "Mary", "Mark", "Den" } for k,v in RandomPairs(Names) do print( v ) end 1 - Mary 2 - John 3 - Den 4 - Mark -- This will output the names, but in a random order each time. -- It could look like: -- Den -- Mary -- Mark -- John -- Or, it could look like: -- John -- Den -- Mary -- Mark -- And so on and so forth. ``` ## Usage of "RandomPairs" ```lua Names = {"John","Mary","Mark","Den"} for key,value in RandomPairs(Names) do print(value) ### SortedPairs `SortedPairs` lets you loop over all the key/value pairs in table, but it sorts the **keys** alphabetically. This is most useful for tables with string keys. ```lua -- Declare our table with some non-alphabetical-order string keys: Names = { E = 5, B = 2, A = 1, D = 4, C = 3, } -- Loop through with SortedPairs this time, and do the standard printing of 'k == v' for k,v in SortedPairs(Names) do print( k .. " == " .. v ) end Den Mary Mark John ``` ## Usage of "SortedPairs" ```lua -- This should output: -- A == 1 -- B == 2 -- C == 3 -- D == 4 -- E == 5 -- Note how SortedPairs sorted the keys alphabetically. Names = {"John","Mary","Mark","Den"} for key,value in SortedPairs(Names) do print(value) end Den John Mark Mary ```

Special Pages


Wikis

?

Render Time: 13ms

DB GetPage 2
Get RevNew 1
Get RevOld 1
Render Body 0
Render Sidebar 7