Creating a Table
Part of the Fundamentals section of Coddy's Lua journey — lesson 57 of 90.
In Lua, tables are created using curly braces {}.
The simplest table you can create is an empty one:
local myTable = {}You can also create a table that already contains values by placing them inside the curly braces, separated by commas:
local numbers = {10, 25, 30}
local names = {"Alice", "Bob", "Charlie"}When you create a table with values like this, Lua automatically assigns each value a numeric position starting from 1. So in the numbers table above, 10 is at position 1, 25 is at position 2, and 30 is at position 3.
Challenge
EasyCreate a game inventory system by building a table to store different weapon types. Declare a variable named weapons and assign it a table containing exactly three string values: "sword", "bow", and "staff". After creating the table, print the entire table to display your weapon collection (given).
Cheat sheet
Tables in Lua are created using curly braces {}.
Create an empty table:
local myTable = {}Create a table with values (separated by commas):
local numbers = {10, 25, 30}
local names = {"Alice", "Bob", "Charlie"}Lua automatically assigns numeric positions starting from 1 to values in tables.
Try it yourself
-- TODO: Write your code here
-- Print the weapons table - DONT MODIFY
print(table.concat(weapons, "\t"))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 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