gawk: Delete
1
1 8.4 The 'delete' Statement
1 ==========================
1
1 To remove an individual element of an array, use the 'delete' statement:
1
1 delete ARRAY[INDEX-EXPRESSION]
1
1 Once an array element has been deleted, any value the element once
1 had is no longer available. It is as if the element had never been
1 referred to or been given a value. The following is an example of
1 deleting elements in an array:
1
1 for (i in frequencies)
1 delete frequencies[i]
1
1 This example removes all the elements from the array 'frequencies'.
1 Once an element is deleted, a subsequent 'for' statement to scan the
1 array does not report that element and using the 'in' operator to check
1 for the presence of that element returns zero (i.e., false):
1
1 delete foo[4]
1 if (4 in foo)
1 print "This will never be printed"
1
1 It is important to note that deleting an element is _not_ the same as
1 assigning it a null value (the empty string, '""'). For example:
1
1 foo[4] = ""
1 if (4 in foo)
1 print "This is printed, even though foo[4] is empty"
1
1 It is not an error to delete an element that does not exist.
1 However, if '--lint' is provided on the command line (⇒Options),
1 'gawk' issues a warning message when an element that is not in the array
1 is deleted.
1
1 All the elements of an array may be deleted with a single statement
1 by leaving off the subscript in the 'delete' statement, as follows:
1
1 delete ARRAY
1
1 Using this version of the 'delete' statement is about three times
1 more efficient than the equivalent loop that deletes each element one at
1 a time.
1
1 This form of the 'delete' statement is also supported by BWK 'awk'
1 and 'mawk', as well as by a number of other implementations.
1
1 NOTE: For many years, using 'delete' without a subscript was a
1 common extension. In September 2012, it was accepted for inclusion
1 into the POSIX standard. See the Austin Group website
1 (http://austingroupbugs.net/view.php?id=544).
1
1 The following statement provides a portable but nonobvious way to
1 clear out an array:(1)
1
1 split("", array)
1
1 The 'split()' function (⇒String Functions) clears out the
1 target array first. This call asks it to split apart the null string.
1 Because there is no data to split out, the function simply clears the
1 array and then returns.
1
1 CAUTION: Deleting all the elements from an array does not change
1 its type; you cannot clear an array and then use the array's name
1 as a scalar (i.e., a regular variable). For example, the following
1 does not work:
1
1 a[1] = 3
1 delete a
1 a = 3
1
1 ---------- Footnotes ----------
1
1 (1) Thanks to Michael Brennan for pointing this out.
1