Last active
May 13, 2022 23:18
-
-
Save birchb1024/9db675f77284e98b5a79ddb26d7d8cfe to your computer and use it in GitHub Desktop.
awk mutliarrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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