8.1.4 Basic Array Example
The following program takes a list of lines, each beginning with a line
number, and prints them out in order of line number. The line numbers
are not in order when they are first read—instead they
are scrambled. This program sorts the lines by making an array using
the line numbers as subscripts. The program then prints out the lines
in sorted order of their numbers. It is a very simple program and gets
confused upon encountering repeated numbers, gaps, or lines that don’t
begin with a number:
| {
if ($1 > max)
max = $1
arr[$1] = $0
}
END {
for (x = 1; x <= max; x++)
print arr[x]
}
|
The first rule keeps track of the largest line number seen so far;
it also stores each line into the array arr
, at an index that
is the line’s number.
The second rule runs after all the input has been read, to print out
all the lines.
When this program is run with the following input:
| 5 I am the Five man
2 Who are you? The new number two!
4 . . . And four on the floor
1 Who is number one?
3 I three you.
|
Its output is:
| 1 Who is number one?
2 Who are you? The new number two!
3 I three you.
4 . . . And four on the floor
5 I am the Five man
|
If a line number is repeated, the last line with a given number overrides
the others.
Gaps in the line numbers can be handled with an easy improvement to the
program’s END
rule, as follows:
| END {
for (x = 1; x <= max; x++)
if (x in arr)
print arr[x]
}
|