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 access a value from a dictionary-style table, use bracket notation — write the table name followed by the key in square brackets and quotes:
print(gameCharacter["playerName"])  -- prints: Hero
print(gameCharacter["level"])       -- prints: 5
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.

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