Skip to content

Instantly share code, notes, and snippets.

@birchb1024
Last active May 13, 2022 23:18
Show Gist options
  • Save birchb1024/9db675f77284e98b5a79ddb26d7d8cfe to your computer and use it in GitHub Desktop.
Save birchb1024/9db675f77284e98b5a79ddb26d7d8cfe to your computer and use it in GitHub Desktop.
awk mutliarrays
#
# multi-dimension arrys
#
# pprint() function - print arbtrary objects
#
function ps(n)
{
for(i=1;i<=n;i++)
printf(" ")
}
function walk_array(arr, name, i)
{
for (i in arr) {
if (isarray(arr[i]))
walk_array(arr[i], (name "[" i "]"))
else
printf("%s[%s] = %s\n", name, i, arr[i])
}
}
function pprint(a, prefix, depth)
{
if(!isarray(a)) {
print ps(depth) prefix " = " a
return
}
for(i in a) {
pprint(a[i], prefix "[" i "]", depth+1)
}
}
function setarray(a, idx, b)
{
if(isarray(b))
for(i in b)
if(isarray(b[i]))
setarray(a[idx], idx, b[i])
else
a[idx][i] = b[i]
}
BEGIN {
a[1][1] = "a11"
a[1][2] = "a12"
a[2] = "a2"
b[1] = "b1"
b[2] = "b2"
c[1] = "c1"
c[2] = "c2"
setarray(b, 3, c)
setarray(a, 3, b)
pprint(a, "a")
pprint(a[1])
pprint(PROCINFO, "PROCINFO")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment