Accessing Elements by Index
Part of the Fundamentals section of Coddy's Lua journey — lesson 58 of 90.
To access a specific element in a list-style table, you use square brackets with the position number of the item you want.
Here's the basic syntax:
local items = {"sword", "shield", "potion"}
print(items[1]) -- outputs: sword
print(items[2]) -- outputs: shieldThere's something very important to remember about Lua: table indices start at 1, not 0. This is different from many other programming languages where the first element is at position 0. In Lua, the first element is always at index 1, the second at index 2, and so on.
local scores = {100, 85, 92}
print(scores[1]) -- first score: 100
print(scores[3]) -- third score: 92This indexing system makes Lua tables feel more natural when thinking about positions - the first item is at position 1, just like you'd count in everyday life.
Challenge
EasyCreate a treasure chest inventory system that demonstrates table element access. First, create a table named treasureChest containing exactly four string values: "gold coin", "ruby gem", "magic scroll", and "silver key". After creating the table, use square bracket notation to access and print the third item from the treasure chest.
Cheat sheet
To access a specific element in a table, use square brackets with the position number:
local items = {"sword", "shield", "potion"}
print(items[1]) -- outputs: sword
print(items[2]) -- outputs: shieldImportant: Lua table indices start at 1, not 0. The first element is at index 1, the second at index 2, and so on.
local scores = {100, 85, 92}
print(scores[1]) -- first score: 100
print(scores[3]) -- third score: 92Try it yourself
-- TODO: Create the treasureChest table with the four items
-- TODO: Access and print the third item from the treasure chestThis 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