table.insert()
Part of the Fundamentals section of Coddy's Lua journey — lesson 61 of 90.
The table.insert() function adds a new element to the end of a list-style table. Here's the basic syntax:
local tasks = {"homework", "shopping"}
table.insert(tasks, "cooking")
print(tasks[3]) -- outputs: cookingWhen you use table.insert(), it automatically places the new element at the next available position and increases the table's length. This means you don't have to worry about calculating the correct index - Lua handles it for you.
local players = {"Alice", "Bob"}
print(#players) -- outputs: 2
table.insert(players, "Charlie")
print(#players) -- outputs: 3Challenge
EasyCreate a quest management system that demonstrates adding new tasks to an adventure log. First, create a table named questLog containing exactly three string values: "Find the ancient artifact", "Defeat the dragon", and "Rescue the villagers". After creating the table, use table.insert() to add a new quest "Collect rare herbs" to the end of the quest log. Finally, print the fourth element of the table to confirm the new quest was added successfully.
Cheat sheet
The table.insert() function adds a new element to the end of a list-style table:
local tasks = {"homework", "shopping"}
table.insert(tasks, "cooking")
print(tasks[3]) -- outputs: cookingThe function automatically places the new element at the next available position and increases the table's length:
local players = {"Alice", "Bob"}
print(#players) -- outputs: 2
table.insert(players, "Charlie")
print(#players) -- outputs: 3Try it yourself
-- TODO: Write your code here
-- Create the questLog table with the three initial quests
-- Add the new quest using table.insert()
-- Print the fourth elementThis 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 Chest10Tables Part 1: List-Style
What is a Table?Creating a TableAccessing Elements by IndexModifying Elements by IndexThe Length Operator '#'table.insert()table.remove()Recap - Manage Party Members2Variables 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 Username3Operators 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