Difference between revisions of "Basic lua: Tables"

From The Official Visionaire Studio: Adventure Game Engine Wiki
 
(2 intermediate revisions by the same user not shown)
Line 242: Line 242:
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| ''Quick note: Whenever you insert a new index entry into a table containing index values it will automatically adjust all of the index values - the same thing also happens when you remove an entry too.''
+
| ''Quick note: Whenever you insert a new index entry into a table containing index values it will automatically adjust all of the index values; the same thing also happens whenever you remove an entry.''
 
|}
 
|}
 
<hr>
 
<hr>
Line 272: Line 272:
 
=== Table Sort ===
 
=== Table Sort ===
  
 +
{| class="ts"
 +
|-
 +
! sort index table alphabetically (ascending) !!
 +
|-
 +
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 +
local t = {"seven", "ate", "nine", "and", "one", "ran away" }
 +
 +
print("before: " .. table.concat(t, ", ") )
 +
 +
table.sort(t) -- sort alphabetically (ascending a, b, c, etc)
 +
 +
print("after: " .. table.concat(t, ", ") )
 +
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(tables)_14.png|frameless|center|180px]]
 +
|}
 +
<hr>
 +
{| class="ts"
 +
|-
 +
| ''Quick note: Mixed data tables will return an error if you try to sort them with the table.sort function, the tables need to contain strings '''or''' numbers, & not both at the same time; however you can get around it by inserting the numbers in as strings.''
 +
|}
 +
<hr>
 +
{| class="ts"
 +
|-
 +
! sort index table alphabetically (descending) !!
 +
|-
 +
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 +
local t = {"7", "ate", "9", "and", "1", "ran away" }
 +
 +
print("before: " .. table.concat(t, ", ") )
 +
 +
table.sort(t, function(a, b) return a > b end) -- sort alphabetically (descending c, b, a, etc)
 +
 +
print("after: " .. table.concat(t, ", ") )
 +
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(tables)_15.png|frameless|center|180px]]
 +
|}
 +
 +
=== Table Count ===
 +
 +
{| class="ts"
 +
|-
 +
! get total amount of entries in index table (current level) !!
 +
|-
 +
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 +
local t = {1, 2, 3, 4}
 +
 +
print( #t )
 +
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(tables)_16.png|frameless|center|180px]]
 +
|}
 +
<hr>
 +
 +
{| class="ts"
 +
|-
 +
! get total amount of entries in keyword table (current level) !!
 +
|-
 +
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 +
local t = {
  
 +
["one"] = 1,
 +
["two"] = 4/2,
 +
["three"] = 3
 +
 +
}
 +
 +
function countKeys(tbl)
 +
  local keys = 0 -- init keys variable
 +
  for k, v in pairs(tbl) do -- iterate through table
 +
    keys = keys + 1 -- increment key value by 1 per key found
 +
  end
 +
  return keys -- return the total amount of keys found
 +
end
 +
 +
print( countKeys(t) )
 +
 +
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(tables)_17.png|frameless|center|180px]]
 +
|}
 
<hr>
 
<hr>
 
{| class="in"
 
{| class="in"

Latest revision as of 01:41, 4 September 2022

< Index >

Tables are one of the features of Lua script, that make the scripting language so dynamic & easy to use, as they allow us to easily create tables, insert, remove & sort data. Tables are often comprised of arrays that usually involve keywords - or an index number - & a value. Tables can be accessed using multiple different methods.

Creating Tables

Below I am going to show you how to create various different types of tables/arrays.

Index Tables

method 1: automatically generated index values
local t = {1, 2, 3, 4, 5, "six", 7, "ate", 9} -- automatically assigns an index number to each value starting from 1
local str = "" -- empty string

for i = 1, #t do -- for 1 to table total, print value of index number
 str = str .. t[i] -- add current table index value to existing text data in the str variable
 if i < #t then str = str .. ", " end -- if i is less than table entry total then insert ", " into the str variable
end

print("table data: " .. str) -- print the table data
print( "table entry total: " .. #(t) ) -- print total of table entries
Basic lua (tables) 1.png

method 2: manually created index values
local t = {

[1] = 1,
[2] = "two",
[3] = 3,
[4] = 2 * 2,
[5] = "five",

}

local str = "" -- empty string

for i = 1, #t do -- for 1 to table total, print value of index number
 str = str .. t[i] -- add current table index value to existing text data in the str variable
 if i < #t then str = str .. ", " end -- if i is less than table entry total then insert ", " into the str variable
end

print("table data: " .. str) -- print the table data
print( "table entry total: " .. #(t) ) -- print total of table entries
Basic lua (tables) 2.png

Keyword Tables

keywords & values
local t = {

["hello world"] = "hello world",
example = "this also works",
camelCaseExample = "this will also work",
snake_case_example = "as will this"

}

for k, v in pairs(t) do
  print(k ..": " .. v)
end

print("\n...but as you can see it does not necessarily return them in the order they were created")
Basic lua (tables) 3.png

Index Values and Sub-Arrays

auto generated index entries with sub-arrays containing keywords
local t = {

{x = 100, y = 300},
{x = 200, y = 111}

}

print("x = " .. t[1].x .. ", y = " .. t[1].y)
Basic lua (tables) 4.png

Keywords and Sub-Arrays

keyword tables with index based sub-arrays containing keywords
local t_texts = {

["English"] = {
    {txt = "hello world!"},
    {txt = "goodbye world!"}
},

["German"] = {
  {txt = "hallo welt!"},
  {txt = "auf wiedersehen welt"}
}

}

print( t_texts["English"][1].txt )
Basic lua (tables) 5.png

Advanced Methods and Table Functions

Below are various table functions & advanced methods for interacting with tables.

Table Concatenation

table concatenation without any formatting (index tables only)
local t = {1, 2, 3, 4}

print( table.concat(t) )
Basic lua (tables) 6.png

table concatenation with formatting (index tables only)
local t = {1, 2, 3, 4}

print( table.concat(t, ", ") )
Basic lua (tables) 7.png

table concatenation with formatting & specified table index range (index tables only)
local t = {1, 2, 3, 4}

print( table.concat(t, ", ", 2, 4) )
Basic lua (tables) 8.png

Table Insert

insert a value into index position 2 in the specified table (index tables only)
local t = {"one", "three"}

print("before: " .. table.concat(t, ", ") )

table.insert(t, 2, "two") -- insert "two" into index position 2

print("after: " .. table.concat(t, ", ") )
Basic lua (tables) 9.png

append a value to the end of the specified table (index tables only)
local t = {"one", "two", "three"}

print("before: " .. table.concat(t, ", ") )

table.insert(t, "four") -- insert "four" at the end of the table

print("after: " .. table.concat(t, ", ") )
Basic lua (tables) 10.png

insert a keyword & value into a table
local t = {

["one"] = 1,
["three"] = 3

}

local str = "" -- init str variable
for k, v in pairs(t) do str = (str .. v .. " ") end -- concatenate data
print("before: " .. str)

t["two"] = 2 -- insert key "two" along with a value into the t table

str = "" -- reset str
for k, v in pairs(t) do str = (str .. v .. " ") end -- concatenate data
print("after: " .. str)
Basic lua (tables) 11.png

Quick note: Technically it's possible to mix & match keyword & index tables, but it's cleaner & less confusing all around if you don't. However feel free to use index values inside of sub-arrays if you don't want to use keywords, or keywords in sub-arrays of index tables.

Table Remove

remove entry from table (index table only)
local t = {1, 2, 3, 4}

print("before: " .. table.concat(t, ", ") )

table.remove(t, 3) -- remove index value 3 from the table

print("after: " .. table.concat(t, ", ") )
Basic lua (tables) 12.png

Quick note: Whenever you insert a new index entry into a table containing index values it will automatically adjust all of the index values; the same thing also happens whenever you remove an entry.

remove keyword entry from specified table
local t = {

["one"] = 1,
["two"] = 2,
["three"] = 3

}

local str = ""
for k, v in pairs(t) do str = (str .. v .. " ") end -- concatenate data
print("before: " .. str)

t["two"] = nil -- remove keyword from table by setting it as nil

str = ""
for k, v in pairs(t) do str = (str .. v .. " ") end -- concatenate data
print("after: " ..str)
Basic lua (tables) 13.png

Table Sort

sort index table alphabetically (ascending)
local t = {"seven", "ate", "nine", "and", "one", "ran away" }

print("before: " .. table.concat(t, ", ") )

table.sort(t) -- sort alphabetically (ascending a, b, c, etc)

print("after: " .. table.concat(t, ", ") )
Basic lua (tables) 14.png

Quick note: Mixed data tables will return an error if you try to sort them with the table.sort function, the tables need to contain strings or numbers, & not both at the same time; however you can get around it by inserting the numbers in as strings.

sort index table alphabetically (descending)
local t = {"7", "ate", "9", "and", "1", "ran away" }

print("before: " .. table.concat(t, ", ") )

table.sort(t, function(a, b) return a > b end) -- sort alphabetically (descending c, b, a, etc)

print("after: " .. table.concat(t, ", ") )
Basic lua (tables) 15.png

Table Count

get total amount of entries in index table (current level)
local t = {1, 2, 3, 4}

print( #t )
Basic lua (tables) 16.png

get total amount of entries in keyword table (current level)
local t = {

["one"] = 1,
["two"] = 4/2,
["three"] = 3

}

function countKeys(tbl)
  local keys = 0 -- init keys variable
  for k, v in pairs(tbl) do -- iterate through table
    keys = keys + 1 -- increment key value by 1 per key found
  end
  return keys -- return the total amount of keys found
end

print( countKeys(t) )
Basic lua (tables) 17.png

< Index >