Removing Pairs with nil
Part of the Fundamentals section of Coddy's Lua journey — lesson 69 of 90.
When you assign nil to a key, Lua effectively removes that key-value pair from the table:
local settings = {
["volume"] = 80,
["difficulty"] = "normal",
["music"] = true
}
-- Remove the difficulty setting
settings.difficulty = nil
-- Now the difficulty key no longer exists in the tableYou can use either dot notation or bracket notation to remove a key-value pair. Both settings.difficulty = nil and settings["difficulty"] = nil work exactly the same way.
After assigning nil to a key, that key completely disappears from the table. If you try to access it later, you'll get nil as the result, just as if the key never existed in the first place.
Challenge
EasyCreate a game configuration system that demonstrates removing unwanted settings from a dictionary-style table. Start by creating a table named gameConfig with the following key-value pairs: ["graphics"] should be assigned the string "high", ["sound"] should be assigned the number 75, ["difficulty"] should be assigned the string "normal", and ["debug"] should be assigned the boolean true.
After creating the initial configuration table, remove the debug setting by assigning nil to it using dot notation. Then print the remaining sound setting to verify the table still contains the other values.
Cheat sheet
To remove a key-value pair from a table, assign nil to the key:
local settings = {
["volume"] = 80,
["difficulty"] = "normal",
["music"] = true
}
-- Remove the difficulty setting
settings.difficulty = nilYou can use either dot notation or bracket notation:
settings.difficulty = nil
settings["difficulty"] = nilAfter assigning nil, the key completely disappears from the table. Accessing it returns nil.
Try it yourself
-- TODO: Write your code here
-- Create the gameConfig table with the required key-value pairs
-- Remove the debug setting using dot notation
-- Print the sound settingThis 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