Multidimensional arrays
AWK supports one-dimensional arrays only. However,we can simulate multidimensional arraysusing one-dimensional arrays.Let us create a multidimensional array as follows:
$ vi multi_arr1.awk BEGIN{ arr["1,1"] = 10 arr["1,2"] = 20 arr["2,1"] = 30 arr["2,2"] = 40 arr["3,1"] = 50 arr["3,2"] = 60 for ( v in arr )
print "Index ",v, " contains "arr[v] } <strong>$ awk -f multi_arr1.awk
The output of the execution of the preceding code is as follows:
Index 1,1 contains 10 Index 1,2 contains 20 Index 2,1 contains 30 Index 2,2 contains 40 Index 3,1 contains 50 Index 3,2 contains 60
In the preceding example, we have given the arr["1,1"]
array as the index. It is not two indexes, as would be the case in a true multidimensional array in other programming languages. It is just one index with the"1,1"
string. So, AWK concatenates the index value and makes them a single string index in the case of multidimensional arrays as well...