The Iterator in AWK
Abstract the traversal of complex associative arrays and simulated lists.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Array-Walking
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# Iterator initialization
function iter_init(arr_name) {
_iter_idx[arr_name] = 1
}
# Check for remaining elements
function iter_has_next(arr, arr_name) {
return _iter_idx[arr_name] <= length(arr)
}
# Retrieve and advance
function iter_next(arr, arr_name, val) {
val = arr[_iter_idx[arr_name]]
_iter_idx[arr_name]++
return val
}
BEGIN {
grimoire[1] = "Chapter 1: The Void"
grimoire[2] = "Chapter 2: The Spark"
grimoire[3] = "Chapter 3: The Flame"
iter_init("grimoire")
while(iter_has_next(grimoire, "grimoire")) {
print "Reading... " iter_next(grimoire, "grimoire")
}
}
AWK natively iterates over associative arrays in a random hash order using for (k in arr). To enforce deterministic, sequential traversal over ordered text collections, an Iterator abstraction manages internal indexing state, guaranteeing chronological processing.
Runic Tags:
#awk
#text-processing
#behavioral