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
pack[1]is the first element;#packcounts.table.insertandremoveshift neighbors as needed.table.concatjoins;table.sortorders in place.
Keywords and builtins used here
localprint
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.
Step 1 of 4 in Tables, step 7 of 27 in Language basics.