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