DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk.info) Reference to Elements

Info Catalog (gawk.info) Array Intro (gawk.info) Arrays (gawk.info) Assigning Elements
 
 Referring to an Array Element
 =============================
 
    The principal way of using an array is to refer to one of its
 elements.  An array reference is an expression which looks like this:
 
      ARRAY[INDEX]
 
 Here, ARRAY is the name of an array.  The expression INDEX is the index
 of the element of the array that you want.
 
    The value of the array reference is the current value of that array
 element.  For example, `foo[4.3]' is an expression for the element of
 array `foo' at index `4.3'.
 
    If you refer to an array element that has no recorded value, the
 value of the reference is `""', the null string.  This includes elements
 to which you have not assigned any value, and elements that have been
 deleted ( The `delete' Statement Delete.).  Such a reference
 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'.)
 
    You can find out if an element exists in an array at a certain index
 with the expression:
 
      INDEX in ARRAY
 
 This expression tests whether or not the particular index exists,
 without the side effect of creating that element if it is not present.
 The expression has the value one (true) if `ARRAY[INDEX]' exists, and
 zero (false) if it does not exist.
 
    For example, to test whether the array `frequencies' contains the
 index `2', you could write this statement:
 
      if (2 in frequencies)
          print "Subscript 2 is present."
 
    Note that this is _not_ a test of whether or not 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
 would do so:
 
      if (frequencies[2] != "")
          print "Subscript 2 is present."
 
Info Catalog (gawk.info) Array Intro (gawk.info) Arrays (gawk.info) Assigning Elements
automatically generated byinfo2html