/
Lua tables as dictionaries

Lua tables as dictionaries

Lua tables can be used as dictionaries with key=value pairs.

Try copy pasting this code into the translator to see how it performs:

local Map = {name="Mary", age=10, height=6.3} trace(Map) trace(Map.name); trace(Map["name"]); for K, V in pairs(Map) do trace(K,V); end trace(#Map.." count for a dictionary table.");

This sample code shows how we define the table, access it’s key values, and iterate through it.

Instead of using the # operator to iterate through the table, like we would with Lua table as list, now we can use the pairs() function to create a for loop, iterating through the table and returning the key-value pairs in the table.

Tip: Notice you can access a table field using the dot notation (Map.name) or square bracket notation (Map[“name“]). It traces the same value.

 

Related content

Lua table as list
Lua table as list
More like this
Value Types
More like this
for loop
Read with this
for in loop
More like this
Auto-Indentation
Auto-Indentation
Read with this
Meta tables
Meta tables
More like this