tables v1.0.1

A utility module for table operations in ComputerCraft providing common table manipulation functions like searching, counting, copying, and comparison operations. Features: Element existence checking with includes(), table size counting for any table type, deep recursive copying with nested table support, deep recursive equality comparison, and works with both array-like and associative tables.

Installation

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua tables
View on GitHub →

This pattern downloads and runs libraries at runtime, automatically installing any that are missing:

-- Auto-install and require libraries
local libs = {"tables"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local tables = require(libDir .. "tables")

-- Use the library
-- (your code here)

Alternative: Direct download via wget:

wget https://raw.githubusercontent.com/Twijn/cc-misc/main/util/tables.lua

Examples

Using with Runtime Installation

This example shows how to download and use libraries with automatic installation:

-- Auto-install and require libraries
local libs = {"tables"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true

for _, lib in ipairs(libs) do
    if not fs.exists(libDir .. lib .. ".lua") then
        allExist = false
        break
    end
end

if not allExist then
    shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end

local tables = require(libDir .. "tables")

-- Use the library

Usage Examples

local tables = require("tables")
local myTable = {1, 2, 3, nested = {a = 1}}
print(tables.includes(myTable, 2)) -- true
local copy = tables.recursiveCopy(myTable)
print(tables.recursiveEquals(myTable, copy)) -- true

Functions

tables.includes(table, object)

View source on GitHub

Check if a table contains a specific value

Parameters:
Returns: boolean # True if the object is found in the table

tables.count(table)

View source on GitHub

Count the number of elements in a table (works with both arrays and associative tables)

Parameters:
Returns: number # The number of key-value pairs in the table

tables.recursiveCopy(table)

View source on GitHub

Create a deep copy of a table, recursively copying all nested tables

Parameters:
Returns: table # A new table with all values copied (nested tables are also copied)

tables.recursiveEquals(t1, t2)

View source on GitHub

Compare two tables for deep equality, recursively checking nested tables

Parameters:
Returns: boolean # True if both tables have the same structure and values