0

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?

Ian
  • 1,507
  • 3
  • 21
  • 36
  • Awk only permits arrays of scalars, and doesn't really support multi-dimensional arrays, as is well explained https://stackoverflow.com/questions/14280877/multidimensional-arrays-in-awk#14281120 – mevets Aug 24 '20 at 13:03
  • Hi @mevets. Do I misunderstand, then, that modern implementations (GNU Awk 5.1.0 here) do properly support multi-dimensional arrays? – Ian Aug 24 '20 at 13:06
  • Yeah, that isn't awk, its gawk. Sorta like C is to C++; most likely anything goes, and some of it might work... – mevets Aug 24 '20 at 13:18
  • 1
    Gnu awk supports multidimensional arrays but not copying them. Being fed up with `for` I've used `asort` to copy 1D arrays (yeah, makes no sense) but I doubt that it would work for copying multidimensional arrays. I've ran into this at some point. Not sure if I ever used it, though: https://unix.stackexchange.com/questions/456315/clone-complex-array-in-awk – James Brown Aug 24 '20 at 13:19

1 Answers1

3

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 (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?.

James Brown
  • 36,089
  • 7
  • 43
  • 59
Ed Morton
  • 188,023
  • 17
  • 78
  • 185