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"]) -- 20Use the table name followed by the key in square brackets and quotes to access the value stored at that key.
Challenge
EasyCreate 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 notationThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username11Tables 2: Dictionary-Style
Key-Value PairsCreating Dictionary-Style TablAccessing - Bracket NotationAccessing with Dot NotationAdding and Modifying PairsRemoving Pairs with nilRecap - Item Properties3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator