[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
cycle Cycle Patterns
The cycle and next statements allow the user to control the execution
of the QTAwk outer loop which reads records from the current input
file and compares them against the patterns. Both statements, restart
the pattern matching.
The cycle statement may use the current input record or the next input
record for restarting the outer pattern matching loop. As each input
record is read from the current input file, the built-in variable
CYCLE_COUNT is set to one. The cycle statement increments the numeric
value of CYCLE_COUNT by one and compares the new value to the numeric
value of the built-in MAX_CYCLE variable. One of two actions is taken
depending on the result of this comparison:
1. If CYCLE_COUNT is greater than MAX_CYCLE, then the next input record
is read, setting NR, FNR, $0, NF and the record fields $1, $2, ...
$NF, before restarting the outer pattern matching loop.
2. If CYCLE_COUNT is less than or equal to MAX_CYCLE, the current
values of NR, FNR, $0, NF and the record fields are utilized when
restarting the outer pattern matching loop.
The default value of MAX_CYCLE is 100. Both CYCLE_COUNT and MAX_CYCLE
are built-in variables and may be set by the user's utility. Setting
MAX_CYCLE is useful to control the number of iterations possible on a
record. If the value of CYCLE_COUNT is set by the user's utility, care
should be taken to prevent the possibility of the utility entering a
loop from which it cannot exit.
The cycle statement is useful when it is necessary to process the
current input record thorugh the outer pattern match loop more than
once. The following utility is a trivial example of one such use.
This utility will print each record with the record number multiple
times. The number of times is determined by the value assigned
MAX_CYCLE in the 'BEGIN' action.
BEGIN {
MAX_CYCLE = 10;
}
{
print FNR,$0;
cycle;
}
See Also:
exit
endfile
next
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson