Let's look at this file:9=foo 3=bar 84=baz 30=bin 71=bon9=goo 3=gar 84=gaz 30=gin 71=gon9=soo 3=sar 84=saz 30=sin 71=sonRunning this gawk line:gawk '{    split($0,arr)    for(i=1;i<=length(arr);i++){        eq=index(arr[i],=)        num=substr(arr[i],eq+1)        val=substr(arr[i],0,eq-1)        printf %s=%s , num,val        arr2[i][num] = val    }    printf ORS}END{    print ---\n,arr2[2][9]}' newfile.txtWhat I expect to get is goo because the first index of the array is the second line, and the second index is the number before the = sign.examples:arr2[1][3] = bararr2[1][71] = bonarr[3][30] = sinso on..Can anyone tell me why it's not working and if it's even possible?gawk version GNU Awk 4.1.1, API: 1.1Thanks.
Can a string be used as index in array of arrays in gawk?
gawk
Yes, it is possible. The problem in your script, however is that you're not doing what (I think) you think you're doing. First of all, you are using i as the index of the first level array:arr2[i][num] = valThis means that i will be a number from 1 to the length of the array and that arr2[i] will be overwritten if any of your lines has the same string value in the same field.Now, the reason you're getting a blank line as output (I assume that's what you're getting, you haven't actually said) is that you are using the wrong order in your array. You have:arr2[i][num] = valSo, for example:arr2[1][soo]=9What you seem to be expecting is the other way around:arr2[1][9]=sooSo, what you need is:arr2[i][value]=numIf we also change the array definition to use NR as a primary index instead of num to avoid collisions, we get:gawk '{    split($0,arr)    for(i=1;i<=length(arr);i++){        eq=index(arr[i],=)        num=substr(arr[i],eq+1)        val=substr(arr[i],0,eq-1)        arr2[NR][val] = num    }}END{  for(i in arr2){    for (num in arr2[i]){      printf arr2[%s][%s]=%s\n, i, num, arr2[i][num]    }  }}' newfile.txtarr2[1][3]=bararr2[1][9]=fooarr2[1][30]=binarr2[1][71]=bonarr2[1][84]=bazarr2[2][3]=gararr2[2][9]=gooarr2[2][30]=ginarr2[2][71]=gonarr2[2][84]=gazarr2[3][3]=sararr2[3][9]=sooarr2[3][30]=sinarr2[3][71]=sonarr2[3][84]=sazAs you can see, arr2[2][9] is now goo as expected. The whole thing is a bit too complex though. You could simplify it to:$ awk -F'[ =]' '{                    for(i=1;i<=NF;i+=2){                        arr2[NR][$(i)]=$(i+1);                    }                } END{print  arr2[2][9]}' newfile.txt goo