Search code examples
awk

How do I assign an array to an element of a multidimensional array?


GNU Awk 5.1.0

I'm building up an associative array using operations such as:

ptf["sysmodName"] = strip(a[1])
ptf["sysmodType"] = strip(a[2])

When I've finished building this, I'd like to assign it to an array of arrays using something like:

x = ptf["sysmodName"]
ptfs[x] = ptf

but awk complains:

 fatal: attempt to use array `ptf' in a scalar context

Is there an easy way to assign an entire array to an element of a multi-dimentional array in its entirety, or am I obliged to do it an element at a time?


Solution

  • You are obliged to do it 1 element at a time:

    x = ptf["sysmodName"]
    for (y in ptf) {
        ptfs[x][y] = ptf[y]
    }
    

    If ptf[] was a true multi-dimensional array, i.e. an array of arrays as supported by gawk, then see How do you copy a multi-dimensional array (i.e. an array of arrays) in awk?.