typestar

Tables as arrays in Lua

One structure for everything, 1-indexed on the array side.

-- One structure for everything: the table. Arrays are 1-indexed.
local pack = { "rope", "lantern", "rations" }

print("First out: " .. pack[1])
print("Items packed: " .. #pack)

-- insert and remove reshape the array part.
table.insert(pack, "grapnel")
table.insert(pack, 1, "map")
table.remove(pack, 3)

print(table.concat(pack, ", "))
table.sort(pack)
print("Alphabetized: " .. table.concat(pack, ", "))

How it works

  1. pack[1] is the first element; #pack counts.
  2. table.insert and remove shift neighbors as needed.
  3. table.concat joins; table.sort orders in place.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
406
Tokens
89
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 76 seconds.

Type this snippet

Step 1 of 4 in Tables, step 7 of 27 in Language basics.

← Previous Next →