gawk: Reference to Elements

 
 8.1.2 Referring to an Array Element
 -----------------------------------
 
 The principal way to use an array is to refer to one of its elements.
 An "array reference" is an expression as follows:
 
      ARRAY[INDEX-EXPRESSION]
 
 Here, ARRAY is the name of an array.  The expression INDEX-EXPRESSION is
 the index of the desired element of the array.
 
    The value of the array reference is the current value of that array
 element.  For example, 'foo[4.3]' is an expression referencing the
 element of array 'foo' at index '4.3'.
 
    A reference to an array element that has no recorded value yields a
 value of '""', the null string.  This includes elements that have not
 been assigned any value as well as elements that have been deleted
 (SeeDelete).
 
      NOTE: A reference to an element that does not exist _automatically_
      creates that array element, with the null string as its value.  (In
      some cases, this is unfortunate, because it might waste memory
      inside 'awk'.)
 
      Novice 'awk' programmers often make the mistake of checking if an
      element exists by checking if the value is empty:
 
           # Check if "foo" exists in a:         Incorrect!
           if (a["foo"] != "") ...
 
      This is incorrect for two reasons.  First, it _creates_ 'a["foo"]'
      if it didn't exist before!  Second, it is valid (if a bit unusual)
      to set an array element equal to the empty string.
 
    To determine whether an element exists in an array at a certain
 index, use the following expression:
 
      INDX in ARRAY
 
 This expression tests whether the particular index INDX exists, without
 the side effect of creating that element if it is not present.  The
 expression has the value one (true) if 'ARRAY[INDX]' exists and zero
 (false) if it does not exist.  (We use INDX here, because 'index' is the
 name of a built-in function.)  For example, this statement tests whether
 the array 'frequencies' contains the index '2':
 
      if (2 in frequencies)
          print "Subscript 2 is present."
 
    Note that this is _not_ a test of whether the array 'frequencies'
 contains an element whose _value_ is two.  There is no way to do that
 except to scan all the elements.  Also, this _does not_ create
 'frequencies[2]', while the following (incorrect) alternative does:
 
      if (frequencies[2] != "")
          print "Subscript 2 is present."