Menu
Coddy logo textTech

Creating Dictionary-Style Tabl

Part of the Fundamentals section of Coddy's Lua journey — lesson 65 of 90.

To create a dictionary-style table, you use curly braces {} and specify each key-value pair using the ["key"] = value syntax:

local student = {
    ["name"] = "Sarah",
    ["age"] = 20,
    ["major"] = "Computer Science"
}

Each key must be enclosed in square brackets and quotes, followed by the equals sign and the corresponding value. You can mix different data types for the values - strings, numbers, booleans, or even other tables.

local gameCharacter = {
    ["playerName"] = "Hero",
    ["level"] = 5,
    ["isAlive"] = true
}

To read a value from a dictionary-style table, use the same bracket notation with the key name:

print(student["name"])   -- Sarah
print(student["age"])    -- 20

Use the table name followed by the key in square brackets and quotes to access the value stored at that key.

challenge icon

Challenge

Easy

Create a video game character profile using dictionary-style table syntax. Create a table named character that represents a player's avatar with the following key-value pairs: ["name"] should be assigned the string "Phoenix", ["class"] should be assigned the string "Mage", ["level"] should be assigned the number 12, and ["experience"] should be assigned the number 2850. After creating the table, print the character's class using bracket notation.

Cheat sheet

To create a dictionary-style table in Lua, use curly braces {} and specify key-value pairs with ["key"] = value syntax:

local student = {
    ["name"] = "Sarah",
    ["age"] = 20,
    ["major"] = "Computer Science"
}

Keys must be enclosed in square brackets and quotes, followed by equals sign and the value. You can mix different data types for values:

local gameCharacter = {
    ["playerName"] = "Hero",
    ["level"] = 5,
    ["isAlive"] = true
}

Try it yourself

-- TODO: Write your code here
-- Create the character table with the required key-value pairs
-- Then print the character's class using bracket notation
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals