✨ ARCANUM RUNE DIFFICULTY: Archmage Structural

The Composite in AWK

Construct hierarchical data trees within flat associative arrays using SUBSEP.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Composite
ARCANUM SCHOOL: Conjuration // Tree-Weaving
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# Use AWK's multi-dimensional array emulation
function add_child(parent, child) { 
    tree[parent, ++child_count[parent]] = child 
}

# Walk the composite structure recursively
function walk_tree(node, depth,    i, indent) {
    for (i=0; i<depth; i++) indent = indent "  "
    print indent "- " node
    
    for (i = 1; i <= child_count[node]; i++) {
        walk_tree(tree[node, i], depth + 1)
    }
}

BEGIN { 
    add_child("Root", "Branch1")
    add_child("Root", "Branch2")
    add_child("Branch1", "LeafA")
    add_child("Branch1", "LeafB")
    
    print "Manifesting the Data Tree:"
    walk_tree("Root", 0) 
}

AWK does not have native references or objects, but its associative arrays combined with the SUBSEP variable (\034) allow mages to weave complex composite trees and graphs. Recursive functions traverse this flat memory space as if it were a deep forest of linked nodes.

Runic Tags: #awk #text-processing #structural