DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk.info) Delete

Info Catalog (gawk.info) Scanning an Array (gawk.info) Arrays (gawk.info) Numeric Array Subscripts
 
 The `delete' Statement
 ======================
 
    You can remove an individual element of an array using the `delete'
 statement:
 
      delete ARRAY[INDEX]
 
    Once you have deleted an array element, you can no longer obtain any
 value the element once had.  It is as if you had never referred to it
 and had never given it any value.
 
    Here 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'.
 
    If you delete an element, a subsequent `for' statement to scan the
 array will not report that element, and the `in' operator to check for
 the presence of that element will return 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, `""').
 
      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.
 
    You can delete all the elements of an array with a single statement,
 by leaving off the subscript in the `delete' statement.
 
      delete ARRAY
 
    This ability is a `gawk' extension; it is not available in
 compatibility mode ( Command Line Options Options.).
 
    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.
 
    The following statement provides a portable, but non-obvious way to
 clear out an array.
 
      # thanks to Michael Brennan for pointing this out
      split("", array)
 
    The `split' function ( Built-in Functions for String
 Manipulation String Functions.)  clears out the target array first.
 This call asks it to split apart the null string. Since there is no
 data to split out, the function simply clears the array and then
 returns.
 
    *Caution:* Deleting an array does not change its type; you cannot
 delete an array and then use the array's name as a scalar. For example,
 this will not work:
 
      a[1] = 3; delete a; a = 3
 
Info Catalog (gawk.info) Scanning an Array (gawk.info) Arrays (gawk.info) Numeric Array Subscripts
automatically generated byinfo2html