GNU Info

Info Node: (gawk.info)Numeric Array Subscripts

(gawk.info)Numeric Array Subscripts


Next: Uninitialized Subscripts Prev: Delete Up: Arrays
Enter node , (file) or (file)node

Using Numbers to Subscript Arrays
=================================

   An important aspect about arrays to remember is that _array
subscripts are always strings_.  When a numeric value is used as a
subscript, it is converted to a string value before being used for
subscripting (Note: Conversion of Strings and Numbers.).
This means that the value of the built-in variable `CONVFMT' can affect
how your program accesses elements of an array.  For example:

     xyz = 12.153
     data[xyz] = 1
     CONVFMT = "%2.2f"
     if (xyz in data)
         printf "%s is in data\n", xyz
     else
         printf "%s is not in data\n", xyz

This prints `12.15 is not in data'.  The first statement gives `xyz' a
numeric value.  Assigning to `data[xyz]' subscripts `data' with the
string value `"12.153"' (using the default conversion value of
`CONVFMT', `"%.6g"').  Thus, the array element `data["12.153"]' is
assigned the value one.  The program then changes the value of
`CONVFMT'.  The test `(xyz in data)' generates a new string value from
`xyz'--this time `"12.15"'--because the value of `CONVFMT' only allows
two significant digits.  This test fails, since `"12.15"' is a
different string from `"12.153"'.

   According to the rules for conversions (Note: Conversion of Strings
and Numbers.), integer values are always converted to
strings as integers, no matter what the value of `CONVFMT' may happen
to be.  So the usual case of the following works:

     for (i = 1; i <= maxsub; i++)
         do something with array[i]

   The "integer values always convert to strings as integers" rule has
an additional consequence for array indexing.  Octal and hexadecimal
constants (Note: Octal and Hexadecimal Numbers.)
are converted internally into numbers and their original form is
forgotten.  This means, for example, that `array[17]', `array[021]', and
`array[0x11]' all refer to the same element!

   As with many things in `awk', the majority of the time things work
as one would expect them to.  But it is useful to have a precise
knowledge of the actual rules which sometimes can have a subtle effect
on your programs.


automatically generated by info2www version 1.2.2.9