Accessing with Dot Notation
Part of the Fundamentals section of Coddy's Lua journey — lesson 67 of 90.
Lua offers a more convenient alternative called dot notation. This is a cleaner, more readable way to access values when your keys are simple strings.
Instead of writing player["score"], you can simply write player.score:
local player = {
["name"] = "Alex",
["score"] = 1500,
["level"] = 8
}
print(player.name) -- outputs: Alex
print(player.score) -- outputs: 1500Dot notation is what programmers call "syntactic sugar" - it's a more pleasant way to write the same thing. However, it only works when your key is a valid Lua identifier, meaning it contains only letters, numbers, and underscores, and doesn't start with a number.
local book = {
["title"] = "Lua Programming",
["pages"] = 300
}
local bookTitle = book.title
print(bookTitle) -- outputs: Lua ProgrammingChallenge
EasyCreate a library management system that demonstrates accessing book information using dot notation. First, create a table named book with the following key-value pairs: ["title"] should be assigned the string "The Lua Guide", ["author"] should be assigned the string "Roberto Ierusalimschy", ["pages"] should be assigned the number 328, and ["year"] should be assigned the number 2016. After creating the table, use dot notation to access and print the book's title.
Cheat sheet
Lua provides dot notation as a cleaner alternative to bracket notation for accessing table values when keys are simple strings:
local player = {
["name"] = "Alex",
["score"] = 1500,
["level"] = 8
}
print(player.name) -- outputs: Alex
print(player.score) -- outputs: 1500Dot notation only works when your key is a valid Lua identifier (contains only letters, numbers, and underscores, and doesn't start with a number):
local book = {
["title"] = "Lua Programming",
["pages"] = 300
}
local bookTitle = book.title
print(bookTitle) -- outputs: Lua ProgrammingTry it yourself
-- TODO: Create the book table with the required key-value pairs
-- TODO: Use dot notation to access and print the book's titleThis 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