
776
|
Chapter 11: The gawk Programming Language
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
strings are called associative arrays.
*
For example, to count the number of widgets
you have, you could use the following script:
/widget/ { count["widget"]++ } Count widgets
END { print count["widget"] } Print the count
You can use the special for loop to read all the elements of an associative array:
for (item in array)
process array[item]
The index of the array is available as item, while the value of an element of the
array can be referenced as array[item].
You can use the operator in to test that an element exists by testing to see if its
index exists. For example:
if (index in array)
...
tests that array[index] exists, but you cannot use it to test the value of the
element referenced by array[index].
You can also delete individual elements of the array using the delete statement.
(See also the delete entry in “Alphabetical Summary of awk Functions and
Commands,” later in this chapter.)
Escape sequences
Within string and regular-expression constants, the following escape sequences
may be used.
The \x escape sequence is a common extension; it is not part of
POSIX awk.
Octal and Hexadecimal Constants in gawk
gawk allows you to use octal and hexadecimal constants in your program source
code. The form is as in C: octal constants start with a leading ...