Go to the first, previous, next, last section, table of contents.


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, the value of l[0] should have been printed.

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 (see section Command Line Options), gawk will warn about the use of the null string as a subscript.


Go to the first, previous, next, last section, table of contents.