<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=Tutorials%2FTypes%2FTables</id>
	<title>Tutorials/Types/Tables - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.figuramc.org/index.php?action=history&amp;feed=atom&amp;title=Tutorials%2FTypes%2FTables"/>
	<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=Tutorials/Types/Tables&amp;action=history"/>
	<updated>2026-04-14T23:45:46Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.figuramc.org/index.php?title=Tutorials/Types/Tables&amp;diff=128&amp;oldid=prev</id>
		<title>Manuel: Automated upload of converted .txt file.</title>
		<link rel="alternate" type="text/html" href="https://wiki.figuramc.org/index.php?title=Tutorials/Types/Tables&amp;diff=128&amp;oldid=prev"/>
		<updated>2024-09-26T20:45:22Z</updated>

		<summary type="html">&lt;p&gt;Automated upload of converted .txt file.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;A table is a Lua value that can store values in specific keys. The act of getting a value from a table using a key is called “indexing”.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;initialize-table&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Initialize Table ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A table can be created using curly brackets.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local t = {}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;generic-indexing&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Generic Indexing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;table[key]&amp;lt;/code&amp;gt; is the way to index a table. You can either get what is currently at that key, or assign a value to that key. There is no limitation to what can be used as keys or values in a table. If you index a table with an unknown key, it will return &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. You can also use variables as a key to index a table using this method.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local v = 6&lt;br /&gt;
&lt;br /&gt;
t[2] = &amp;quot;number key, string value&amp;quot;&lt;br /&gt;
&lt;br /&gt;
t[&amp;quot;string key, table value&amp;quot;] = {}&lt;br /&gt;
&lt;br /&gt;
t[false] = true&lt;br /&gt;
&lt;br /&gt;
t[v] = &amp;quot;ree&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(t[2]) --&amp;gt; &amp;quot;number key, string value&amp;quot;&lt;br /&gt;
&lt;br /&gt;
print(t[&amp;quot;reeee&amp;quot;]) --&amp;gt; nil&lt;br /&gt;
&lt;br /&gt;
print(t[&amp;quot;string key, table value&amp;quot;]) --&amp;gt; table 3be7a8&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;string-indexing-shorthand&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== String Indexing Shorthand ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If that seems like a lot of work to index by a string, yes it is.&amp;lt;br/&amp;gt; &amp;lt;code&amp;gt;table.key&amp;lt;/code&amp;gt; is the shorthand for indexing a table with a string. This has very specific restrictions for what the string can contain.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Cannot start with a number (&amp;lt;code&amp;gt;t.2fort&amp;lt;/code&amp;gt; will not work. Use &amp;lt;code&amp;gt;[]&amp;lt;/code&amp;gt; indexing, or use a different string)&lt;br /&gt;
&lt;br /&gt;
* Cannot contain spaces, periods, or other special characters&lt;br /&gt;
&lt;br /&gt;
* Cannot be Lua Keywords (true, false, local, function)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;t.name = &amp;quot;Katt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
t.age = -1&lt;br /&gt;
&lt;br /&gt;
t.gender = t.name&lt;br /&gt;
&lt;br /&gt;
t.underscores_are_allowed = true&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;object-oriented-method-indexing&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Object Oriented Method Indexing ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is one more way to index a table. Many of the functions in Figura take in the object that called said function as the first parameter. This is because every object of the same type has the exact same functions. This is done via &amp;lt;code&amp;gt;table:key()&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local posA = player:getPos()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local posB = player.getPos(player)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;initialize-table-with-values&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Initialize Table with Values ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can assign values to keys when the table is initialized. Each key-value pair must be separated by a comma (&amp;lt;code&amp;gt;,&amp;lt;/code&amp;gt;)&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- prettier-ignore --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local a = {&lt;br /&gt;
&lt;br /&gt;
    [false] = 1,&lt;br /&gt;
&lt;br /&gt;
    [&amp;quot;string with spaces&amp;quot;] = &amp;quot;string&amp;quot;,&lt;br /&gt;
&lt;br /&gt;
    [v] = {&lt;br /&gt;
&lt;br /&gt;
        [&amp;quot;a&amp;quot;] = 1,&lt;br /&gt;
&lt;br /&gt;
        [&amp;quot;b&amp;quot;] = 2,&lt;br /&gt;
&lt;br /&gt;
    },&lt;br /&gt;
&lt;br /&gt;
    -- string shorthand rules still apply. This is equivalent to &amp;lt;code&amp;gt;[&amp;quot;life&amp;quot;] = 42&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
    life = 42,&lt;br /&gt;
&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you do not specify an index, the provided values will automatically be assigned a numeric index, starting at &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;. This is how arrays are handled in lua, just a table that acts as an array. A table array if you will. Unlike other languages, Lua arrays begin indexing at &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; and functions that take in an array expect the first element at &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- prettier-ignore --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;local array = {&lt;br /&gt;
&lt;br /&gt;
    42,        -- [1] = 42&lt;br /&gt;
&lt;br /&gt;
    36,        -- [2] = 36&lt;br /&gt;
&lt;br /&gt;
    1024,      -- [3] = 1024&lt;br /&gt;
&lt;br /&gt;
    1,         -- [4] = 1&lt;br /&gt;
&lt;br /&gt;
    &amp;quot;string&amp;quot;,  -- [5] = &amp;quot;string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    v,         -- [6] = v&lt;br /&gt;
&lt;br /&gt;
    t          -- [7] = t&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- newlines are ignored, as with everything in lua&lt;br /&gt;
&lt;br /&gt;
local array2 = { 42, 36, 1024, 1, &amp;quot;string&amp;quot;, v, t }&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;iterating-over-a-table&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Iterating Over a Table ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Iterating over a table is simple.&amp;lt;br/&amp;gt; You can iterate over every single index using &amp;lt;code&amp;gt;pairs&amp;lt;/code&amp;gt;. This will go through every index, but it will be in an undefined order. &amp;lt;code&amp;gt;pairs&amp;lt;/code&amp;gt; has 2 values it returns when used in a for loop: the current &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;, and the current &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt; at that &amp;lt;code&amp;gt;key&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;for key, value in pairs(t) do&lt;br /&gt;
&lt;br /&gt;
    print(key, value)&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If the order of the iteration is important, you can use &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt;, but it only goes over numerical indices. This is what you want to use for table arrays. It starts at index &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;, and increments by &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; until the table returns &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. When used in a for loop, &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt; returns the current index and the &amp;lt;code&amp;gt;value&amp;lt;/code&amp;gt; at that &amp;lt;code&amp;gt;index&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;for index, value in ipairs(array) do&lt;br /&gt;
&lt;br /&gt;
    print(index, value)&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;length-of-table-array&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Length of Table Array ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can use the &amp;lt;code&amp;gt;#&amp;lt;/code&amp;gt; operator to get the length of a table array. For tables with non-numeric indexes, you have to use &amp;lt;code&amp;gt;pairs&amp;lt;/code&amp;gt; and calculate the length yourself, though the “length” of that kind of table isnt really useful. This follows the same rules as &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt; in the way that the table’s length is every numeric index until one returns &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. So &amp;lt;code&amp;gt;#{1,2,3,4}&amp;lt;/code&amp;gt; will return &amp;lt;code&amp;gt;4&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;#{1,2,nil,4}&amp;lt;/code&amp;gt; will return &amp;lt;code&amp;gt;2&amp;lt;/code&amp;gt;.&amp;lt;br/&amp;gt; As an example, &amp;lt;code&amp;gt;ipairs&amp;lt;/code&amp;gt; is pretty much just this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;for index = 1, #array, 1 do&lt;br /&gt;
&lt;br /&gt;
    print(index, array[index])&lt;br /&gt;
&lt;br /&gt;
end&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;manipulating-table-arrays&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Manipulating Table Arrays ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Lua comes built in with ways to manipulate tables. Not all are described here, just the ones that I feel are most important.&amp;lt;br/&amp;gt; All of these functions are available via the &amp;lt;code&amp;gt;tables&amp;lt;/code&amp;gt; global.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;table.insertt-pos-value&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;code&amp;gt;table.insert(t, pos, value)&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This function can add a value at any index, shifting the other values to account for the added value.&amp;lt;br/&amp;gt; &amp;lt;code&amp;gt;table.insert(array, 1, “e”)&amp;lt;/code&amp;gt; will insert &amp;lt;code&amp;gt;“e”&amp;lt;/code&amp;gt; at the beggining of the table &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt;, shifting every other value forward one index.&amp;lt;br/&amp;gt; When adding elements to the end of the array, you use the function as &amp;lt;code&amp;gt;table.insert(t, value)&amp;lt;/code&amp;gt;. So &amp;lt;code&amp;gt;table.insert(array, “l”)&amp;lt;/code&amp;gt; appends &amp;lt;code&amp;gt;“l”&amp;lt;/code&amp;gt; to the end of the table &amp;lt;code&amp;gt;array&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;table.removet-pos&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== &amp;lt;code&amp;gt;table.remove(t, pos)&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This function can remove a value at any index, shifting the other values to account for the removed value. The value that was removed will be returned by this function as well.&amp;lt;br/&amp;gt; &amp;lt;code&amp;gt;table.remove(array, 1)&amp;lt;/code&amp;gt; will remove the value at index &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; from the table, shifting all the values back an index.&amp;lt;br/&amp;gt; &amp;lt;code&amp;gt;pos&amp;lt;/code&amp;gt; is optional, with the default value being &amp;lt;code&amp;gt;#t&amp;lt;/code&amp;gt;. &amp;lt;code&amp;gt;table.remove(array)&amp;lt;/code&amp;gt; will remove the last value in the table.&lt;/div&gt;</summary>
		<author><name>Manuel</name></author>
	</entry>
</feed>