table
Table manipulation utilities.
Table of contents
- Overview
- table.insert(t, value)
- table.insert(t, pos, value)
- table.remove(t, pos)
- table.print(t)
- Example Usage
- Notes
Overview
The table module provides utilities for manipulating tables. It must be explicitly imported:
const table = import("table");
table.insert(arr, 10);
table.insert(t, value)
Appends a value to the end of the table’s array part.
let t = {10, 20};
table.insert(t, 30);
// t is now {10, 20, 30}
print(t[2]); // 30
table.insert(t, pos, value)
Inserts a value at the specified position, shifting other elements.
let t = {10, 20, 30};
table.insert(t, 1, 15);
// t is now {10, 15, 20, 30}
print(t[1]); // 15
Parameters:
t- Table to insert intopos- Position to insert at (0-indexed)value- Value to insert
table.remove(t, pos)
Removes and returns the element at the specified position.
let t = {10, 20, 30};
let val = table.remove(t, 1);
print(val); // 20
print(t[0]); // 10
print(t[1]); // 30 (shifted down)
print(rawlen(t)); // 2
Parameters:
t- Table to remove frompos- Position to remove (0-indexed)
Returns: The removed value
table.print(t)
Debug print of table contents. Useful for inspecting table structure.
let t = {
name = "Alice",
age = 30,
[0] = "first",
[1] = "second"
};
table.print(t);
// Output shows all key-value pairs
Example Usage
const table = import("table");
// Build an array
let numbers = {};
table.insert(numbers, 10);
table.insert(numbers, 20);
table.insert(numbers, 30);
print(rawlen(numbers)); // 3
// Insert in the middle
table.insert(numbers, 1, 15);
// numbers = {10, 15, 20, 30}
// Remove elements
let removed = table.remove(numbers, 2);
print(removed); // 20
// numbers = {10, 15, 30}
// Debug output
table.print(numbers);
// Working with mixed tables
let mixed = {
name = "Config",
[0] = "item1",
[1] = "item2"
};
table.insert(mixed, "item3");
// Array part: {item1, item2, item3}
// Hash part: {name = "Config"}
table.print(mixed);
Notes
table.insertwith one argument appends to the endtable.insertwith position shifts elements to make roomtable.removeshifts remaining elements down- All positions are 0-indexed (unlike Lua’s 1-indexed)
- These functions work on the array part of tables (consecutive integer keys from 0)