O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Multidimensional arrays

AWK supports one-dimensional arrays only. However, we can simulate multidimensional arrays using one-dimensional arrays. Let us create a multidimensional array as follows:

$ vi multi_arr1.awkBEGIN{    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 10Index  1,2  contains 20Index  2,1  contains 30Index  2,2  contains 40Index  3,1  contains 50Index  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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required