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