How to initialize an array of arrays in awk?

If you have GNU awk, you can use a true multidimensional array. Although this answer uses the split() function, it most certainly doesn’t abuse it. Run like:

awk -f script.awk

Contents of script.awk:

BEGIN {

    x=SUBSEP

    a="Red" x "Green" x "Blue"
    b="Yellow" x "Cyan" x "Purple"

    Colors[1][0] = ""
    Colors[2][0] = ""

    split(a, Colors[1], x)
    split(b, Colors[2], x)

    print Colors[2][3]
}

Results:

Purple

Leave a Comment

tech