Menu
Coddy logo textTech

The Length Operator '#'

Part of the Fundamentals section of Coddy's Lua journey — lesson 60 of 90.

Lua provides a simple and convenient way to do this using the length operator #.

The length operator is placed directly in front of a table variable and returns the number of elements in that table:

local players = {"Alice", "Bob", "Charlie"}
print(#players)  -- outputs: 3

This operator works by counting all the consecutive elements starting from index 1. It's particularly useful when you need to know the size of your table for loops, conditions, or simply to display information to users.

local scores = {100, 85, 92, 78, 95}
local totalPlayers = #scores
print("Number of players:", totalPlayers)  -- outputs: Number of players: 5
challenge icon

Challenge

Easy

Create a guild roster management system that tracks the number of active members. First, create a table named guildMembers containing exactly five string values: "Warrior", "Mage", "Archer", "Healer", and "Rogue". After creating the table, use the length operator # to determine how many members are in the guild and print this count.

Cheat sheet

The length operator # returns the number of elements in a table:

local players = {"Alice", "Bob", "Charlie"}
print(#players)  -- outputs: 3

The operator counts consecutive elements starting from index 1:

local scores = {100, 85, 92, 78, 95}
local totalPlayers = #scores
print("Number of players:", totalPlayers)  -- outputs: Number of players: 5

Try it yourself

-- TODO: Create the guildMembers table with the specified values

-- TODO: Use the length operator to count the members

-- TODO: Print the count
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals