0Pricing
Lua Academy · Lesson

Arrays as Tables

1-based indexing in Lua.

Arrays as Tables is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Tables Are Lua's Lists

Lua has one container type: the table. When you store values in order, the table acts like an array or list.

You create an empty table with {} and fill it later, or list values right away inside the braces.

local fruits = {"apple", "banana", "cherry"}
print(fruits)

Indexing Starts at 1

This is the most important rule: Lua arrays are 1-based. The first item lives at index 1, not 0.

Use square brackets to read an element by its position.

local fruits = {"apple", "banana", "cherry"}
print(fruits[1])
print(fruits[2])
print(fruits[3])

Index Zero Is Empty

Because counting begins at 1, position 0 normally holds nothing.

Reading an index that was never set returns nil. Beginners coming from other languages often trip on this.

local colors = {"red", "green", "blue"}
print(colors[0])
print(colors[1])

Writing an Element

Assign to a position with the bracket syntax to set or replace a value.

Here we overwrite the second slot. The change is visible the next time we read that index.

local scores = {10, 20, 30}
scores[2] = 99
print(scores[2])

Building a Table Step by Step

You can start empty and add items at consecutive indices. Remember to begin at 1 to keep a clean sequence.

A sequence has no holes: indices 1, 2, 3 are all filled.

local nums = {}
nums[1] = 5
nums[2] = 10
nums[3] = 15
print(nums[1], nums[2], nums[3])

Mixed Value Types

A Lua table can hold values of different types in the same sequence: numbers, strings, booleans.

Lua does not force every element to share a type the way some languages do.

local mixed = {42, "hello", true}
print(mixed[1])
print(mixed[2])
print(mixed[3])

Printing All Items

To show every element, walk from index 1 upward with a numeric for loop.

We will meet a cleaner way (ipairs) in a later lesson, but the loop makes the 1-based range obvious.

local pets = {"cat", "dog", "fish"}
for i = 1, 3 do
  print(i, pets[i])
end

Nested Tables

An element of a table can itself be a table. This lets you build rows, grids, or grouped data.

Chain brackets to reach inside: grid[1][2] reads row 1, column 2 (both 1-based).

local grid = {
  {1, 2, 3},
  {4, 5, 6}
}
print(grid[1][2])
print(grid[2][3])

First and Last by Position

The first item is always list[1]. For a sequence of known size, the last is at that count.

Here three items means the last sits at index 3.

local week = {"Mon", "Tue", "Wed"}
print("first:", week[1])
print("last:", week[3])

Changing Values In Place

Tables are mutable. You can update any existing slot without rebuilding the whole list.

Below, we raise every score by reassigning each index in a loop.

local scores = {1, 2, 3}
for i = 1, 3 do
  scores[i] = scores[i] + 10
end
print(scores[1], scores[2], scores[3])

Tables as Records vs Arrays

The same table type also stores named keys, like person.name. When keys are the integers 1, 2, 3..., we call it a sequence or array.

This lesson focuses on the sequence style with 1-based numeric keys.

local list = {"a", "b"}
list[3] = "c"
print(list[1], list[2], list[3])

Quick Check

Test your understanding of Lua indexing.

Recap

You learned that Lua uses tables as arrays, and that indexing is 1-based: the first item is at [1].

You can read, write, and nest table elements, and a clean sequence has no gaps. Next you will add and remove items dynamically.

Frequently asked questions

Is the “Arrays as Tables” lesson free?

Yes — the full text of “Arrays as Tables” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arrays as Tables”?

1-based indexing in Lua. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arrays as Tables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays as Tables
  2. Adding and Removing Items
  3. Iterating with ipairs
  4. Length and Sorting
← Back to Lua Academy