I'm able to do this properly in gawk, but when I tried to post my code to the machine where it will run, I realized it was using mawk...
$ cat multidim.gawk
# test of multidimensional arrays
// {
A[1][1]="A11"
A[1][2]="A12"
A[2][1]="A21"
A[2][2]="A22"
i=2
for ( j in A[i] )
{
print "i=" i " j=" j " A[i][j]=" A[i][j]
}
}
$ echo hi | awk -f multidim.gawk
i=2 j=1 A[i][j]=A21
i=2 j=2 A[i][j]=A22
seems mawk has a different idea about how multidimensional arrays should work. When I run it on Debian with mawk, I get a syntax error. A[i,j] seems the correct syntax, and it 'synthesizes' multidimensional arrays.
So I tried two things, neither work:
$ cat multidim.mawk
// {
A[1,1]="A11"
A[1,2]="A12"
A[2,1]="A21"
A[2,2]="A22"
i=2
for ( j in A[i] )
{
print "i=" i " j=" j "a[i,j]=" a[i,j]
}
}
$ echo hi | awk -f multidim.mawk
awk: multidim.mawk: line 9: syntax error at or near [
Seems sensible, using a 1dim array index on a "multidimensional" array generates an error.
Trying to just walk the WHOLE array so that I can use an if statement to selct the first dimension even (extremely inefficient and horrible)... but I can't even do that!:
$ cat multidim2.mawk
# test of multidimensional arrays
// {
A[1,1]="A11"
A[1,2]="A12"
A[2,1]="A21"
A[2,2]="A22"
for ( (i, j) in A )
{
print "i=" i " j=" j "a[i,j]=" a[i,j]
}
}
$ echo hi | awk -f multidim2.mawk
awk: multidim2.mawk: line 8: syntax error at or near )
Is there any way to walk a multidimensional array in mawk?
Is there a language reference other than the mawk manpage?
Thanks!