26

In awk, I can clear an array with a loop, making it an empty array, which is equivalent to deleting it.

for (key in array) delete array[key];

Is there a simpler way? Can I completely delete an array, so that the variable name can be re-used for a scalar?

  • Actually when I tried similar for an associative array, awk --lint displayed a warning like "warning: for loop: array `MRA' changed size from 1 to 0 during loop execution". So probably the code above is a bad idea anyway. – U. Windl Mar 18 '22 at 11:34
  • @U.Windl POSIX states “The results of adding new elements to array within such a for loop are undefined”, and states no such thing about deleting elements, so I think that code is fine. Maybe the GNU awk linter isn't able distinguish adding and removing elements? – Gilles 'SO- stop being evil' Mar 18 '22 at 12:17
  • Well: If the array is traditionally indexed (starting from 0) and say you are removing element #0 in the first iteration, won't element #1 become the new element #0, and if you continue with index 1, won't you miss an element? If the implementation is going to avoid such issues it has to be rather tricky. – U. Windl Mar 21 '22 at 08:00

2 Answers2

46

The syntax

delete array

is not in current versions in POSIX, but it is supported by virtually all existing implementations (including the original awk, GNU, mawk, and BusyBox). It will be added in a future version of POSIX (see defect 0000544).

An alternate way to clear all array elements, which is both portable and standard-compliant, and which is an expression rather than a statement, is to rely on split deleting all existing elements:

split("", array)

All of these, including delete array, leave the variable marked as being an array variable in the original awk, in GNU awk and in mawk (but not in BusyBox awk). As far as I know, once a variable has been used as an array, there is no way to use it as a scalar variable.

Ed Morton
  • 31,617
  • 2
    The alternative split solution was helpful. It was a surprise to find SCO Openserver 5.0.7 awk calls delete array a syntax error though the man page says the subscript is optional. $ awk 'BEGIN { A[1] = 0; delete A; }' awk: Syntax error at line 1 of program << BEGIN { A[1] = 0; de ... >> context is BEGIN { A[1] = 0; delete >>> A; <<< awk: illegal statement at line 1 of program << BEGIN { A[1] = 0; de ... >> It was more surprising to find that delete array[subscript], while not shown as an error, also did not work, and many variants on array[subscript] = "" were all equally ineffective. – kbulgrien Apr 24 '18 at 16:34
  • 5
    Or just split("", array) – ryenus Sep 27 '19 at 04:43
1

Not sure why it'd be a good idea but if you want to use the same name as both an array and a scalar, you can do that if you don't use them in the same scope, e.g.:

$ cat tst.awk
BEGIN {
    fooIsArray()
    foo = 17
    print "foo is a scalar:", foo
}

function fooIsArray( foo) { foo[1] = "bar" print "foo is an array:", foo[1] }

$ awk -f tst.awk
foo is an array: bar
foo is a scalar: 17

In the above we're making foo[] a function-local variable by declaring it as an unused argument to the function fooIsArray() and so it's unrelated to the global scalar variable foo used elsewhere in the script and the contents of the array foo[] will be deleted on return from the function fooIsArray().

Ed Morton
  • 31,617
  • Only function parameter names have a local scope. You can't do this for code in an action. – Gilles 'SO- stop being evil' Feb 04 '22 at 14:32
  • @Gilles'SO-stopbeingevil' right, the only 2 scopes awk has for variables are global or function-local so you'd use a function as I show. Obviously you can call a function in an action block if you like. – Ed Morton Feb 04 '22 at 14:33