Accessing - Bracket Notation
Part of the Fundamentals section of Coddy's Lua journey — lesson 66 of 90.
To access a value in a dictionary-style table, you use the key inside square brackets.
The syntax for accessing values with bracket notation is straightforward:
local player = {
["name"] = "Alex",
["score"] = 1500,
["level"] = 8
}
print(player["name"]) -- outputs: Alex
print(player["score"]) -- outputs: 1500You simply write the table name, followed by square brackets containing the key as a string. This tells Lua to look up that specific key and return its corresponding value.
local monster = {
["type"] = "Dragon",
["hp"] = 200,
["damage"] = 50
}
local currentHealth = monster["hp"]
print(currentHealth) -- outputs: 200Challenge
EasyCreate a dungeon exploration system that demonstrates accessing monster information using bracket notation. First, create a table named monster with the following key-value pairs: ["type"] should be assigned the string "Orc", ["health"] should be assigned the number 75, ["attack"] should be assigned the number 15, and ["defense"] should be assigned the number 8. After creating the table, use bracket notation to access and print the monster's health value.
Cheat sheet
To access values in a dictionary-style table, use the key inside square brackets:
local player = {
["name"] = "Alex",
["score"] = 1500,
["level"] = 8
}
print(player["name"]) -- outputs: Alex
print(player["score"]) -- outputs: 1500You can also store accessed values in variables:
local currentHealth = monster["hp"]
print(currentHealth) -- outputs the hp valueTry it yourself
-- TODO: Create the monster table with the required key-value pairs
-- TODO: Use bracket notation to access and print the monster's health valueThis 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