Adding and Modifying Pairs
Part of the Fundamentals section of Coddy's Lua journey — lesson 68 of 90.
To add a new key-value pair or modify an existing one, you simply use the assignment operator with either dot notation or bracket notation:
local player = {
["name"] = "Alex",
["level"] = 5
}
-- Add a new key-value pair
player.gold = 100
-- Modify an existing value
player.level = 6The beauty of this approach is that Lua doesn't distinguish between adding and modifying. If the key "gold" doesn't exist in the table, it gets created with the value 100. If the key "level" already exists, its value gets updated to 6.
You can use either dot notation or bracket notation for these operations:
-- Both of these work the same way
player.class = "Warrior"
player["weapon"] = "Sword"Challenge
EasyCreate a character progression system that demonstrates both adding new attributes and modifying existing ones. Start by creating a table named hero with the following initial key-value pairs: ["name"] should be assigned the string "Warrior", ["level"] should be assigned the number 5, and ["health"] should be assigned the number 120.
After creating the initial character table, perform the following modifications: First, add a new key-value pair ["class"] with the value "Paladin" using dot notation. Next, modify the existing level value to 7 using bracket notation. Finally, add another new key-value pair ["mana"] with the value 80 using bracket notation.
After all modifications are complete, print the hero's class and the updated level to verify the changes.
Cheat sheet
To add a new key-value pair or modify an existing one in a table, use the assignment operator with dot notation or bracket notation:
local player = {
["name"] = "Alex",
["level"] = 5
}
-- Add a new key-value pair
player.gold = 100
-- Modify an existing value
player.level = 6Lua doesn't distinguish between adding and modifying - if the key doesn't exist, it gets created; if it exists, its value gets updated.
Both dot notation and bracket notation work for these operations:
-- Both work the same way
player.class = "Warrior"
player["weapon"] = "Sword"Try it yourself
-- Create the initial hero table
local hero = {
-- TODO: Add the initial key-value pairs here
}
-- TODO: Add new attributes and modify existing ones using dot and bracket notation
-- Output the hero's class and updated level
print(hero.class)
print(hero.level)This 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