< Day Day Up > |
6.18. LoopsLoops are used to repeatedly execute the statements following the test expression if a condition is true. Loops are often used to iterate through the fields within a record and to loop through the elements of an array in the END block. Awk has three types of loops: while, for, and special-for, which will be discussed later when working with awk arrays. 6.18.1 while LoopThe first step in using a while loop is to set a variable to an initial value. The value is then tested in the while expression. If the expression evaluates to true (nonzero), the body of the loop is entered and the statements within that body are executed. If there is more than one statement within the body of the loop, those statements must be enclosed in curly braces. Before ending the loop block, the variable controlling the loop expression must be updated or the loop will continue forever. In the following example, the variable is reinitialized each time a new record is processed. The do/while loop is similar to the while loop, except that the expression is not tested until the body of the loop is executed at least once. Example 6.130.
% nawk '{ i = 1; while ( i <= NF ) { print NF, $i ; i++ } }' filename
EXPLANATION The variable i is initialized to 1; while i is less than or equal to the number of fields (NF) in the record, the print statement will be executed, then i will be incremented by 1. The expression will then be tested again, until the variable i is greater than the value of NF . The variable i is not reinitialized until awk starts processing the next record. 6.18.2 for LoopThe for loop and while loop are essentially the same, except the for loop requires three expressions within the parentheses: the initialization expression, the test expression, and the expression to update the variables within the test expression. In awk, the first statement within the parentheses of the for loop can perform only one initialization. (In C, you can have multiple initializations separated by commas.) Example 6.131.
% nawk '{ for( i = 1; i <= NF; i++) print NF,$i }' filex
EXPLANATION The variable i is initialized to 1 and tested to see whether it is less than or equal to the number of fields (NF) in the record. If so, the print function prints the value of NF and the value of $i (the $ preceding the i is the number of the ith field), then i is incremented by 1. (Frequently the for loop is used with arrays in an END action to loop through the elements of an array.) See "Arrays" on page 233. 6.18.3 Loop Controlbreak and continue StatementsThe break statement lets you break out of a loop if a certain condition is true. The continue statement causes the loop to skip any statements that follow if a certain condition is true, and returns control to the top of the loop, starting at the next iteration. Example 6.132.(In the Script) 1 {for ( x = 3; x <= NF; x++ ) if ( $x < 0 ){ print "Bottomed out!"; break} # breaks out of for loop } 2 {for ( x = 3; x <= NF; x++ ) if ( $x == 0 ) { print "Get next item"; continue} # starts next iteration of the for loop } EXPLANATION
|
< Day Day Up > |