DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

(gawk.info) Uninitialized Subscripts

Info Catalog (gawk.info) Numeric Array Subscripts (gawk.info) Arrays (gawk.info) Multi-dimensional
 
 Using Uninitialized Variables as Subscripts
 ===========================================
 
    Suppose you want to print your input data in reverse order.  A
 reasonable attempt at a program to do so (with some test data) might
 look like this:
 
      $ echo 'line 1
      > line 2
      > line 3' | awk '{ l[lines] = $0; ++lines }
      > END {
      >     for (i = lines-1; i >= 0; --i)
      >        print l[i]
      > }'
      -| line 3
      -| line 2
 
    Unfortunately, the very first line of input data did not come out in
 the output!
 
    At first glance, this program should have worked.  The variable
 `lines' is uninitialized, and uninitialized variables have the numeric
 value zero.  So, `awk' should have printed the value of `l[0]'.
 
    The issue here is that subscripts for `awk' arrays are *always*
 strings. And uninitialized variables, when used as strings, have the
 value `""', not zero.  Thus, `line 1' ended up stored in `l[""]'.
 
    The following version of the program works correctly:
 
      { l[lines++] = $0 }
      END {
          for (i = lines - 1; i >= 0; --i)
             print l[i]
      }
 
    Here, the `++' forces `lines' to be numeric, thus making the "old
 value" numeric zero, which is then converted to `"0"' as the array
 subscript.
 
    As we have just seen, even though it is somewhat unusual, the null
 string (`""') is a valid array subscript (d.c.). If `--lint' is provided
 on the command line ( Command Line Options Options.), `gawk' will
 warn about the use of the null string as a subscript.
 
Info Catalog (gawk.info) Numeric Array Subscripts (gawk.info) Arrays (gawk.info) Multi-dimensional
automatically generated byinfo2html