Menu
Coddy logo textTech

Index Signatures

Part of the Introduction To Luau section of Coddy's Lua journey — lesson 72 of 73.

You met map types like {[string]: number} back in the tables chapter — an index signature: any string key maps to a number value. They shine when the keys aren't known while you write the code: counters, settings, caches, scores — anything built up at runtime.

type Counts = {[string]: number}

local counts: Counts = {}
counts["potion"] = 3   -- any string key is allowed
counts["sword"] = 1
counts["potion"] += 1  -- values are checked as numbers

Two runtime facts matter when working with maps. First, reading a key that was never set returns nil — the classic counting pattern handles that with or, turning "missing" into 0:

counts[item] = (counts[item] or 0) + 1

Second, pairs() iterates a map's keys in no guaranteed order — running the same program twice can visit keys differently. Whenever output must be predictable, collect the keys into an array, table.sort them, and iterate the sorted array instead:

local keys: {string} = {}
for key in pairs(counts) do
    table.insert(keys, key)
end
table.sort(keys)
for _, key in ipairs(keys) do
    print(`{key}: {counts[key]}`)
end
challenge icon

Challenge

Easy

Count loot drops with a typed map.

  • Declare type Counts = {[string]: number}.
  • Write countItems(items: {string}): Counts that builds a fresh Counts map, counting how many times each string appears (use the (counts[item] or 0) + 1 pattern).
  • Create local loot: {string} = {"potion", "sword", "potion", "shield", "potion", "sword"} and count it.
  • Print one [name]: [count] line per distinct item, in alphabetical order: collect the keys, table.sort them, then print.

Expected output:

potion: 3
shield: 1
sword: 2

Try it yourself

-- Write code here
-- 1) type Counts = {[string]: number}
-- 2) countItems(items: {string}): Counts using (counts[item] or 0) + 1
-- 3) count the loot array from the task
-- 4) collect keys, table.sort them, print `{name}: {count}` lines
quiz iconTest yourself

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

All lessons in Introduction To Luau