gawk: Delete

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