diff options
-rw-r--r-- | cppawk-include/iter.h | 7 | ||||
-rw-r--r-- | cppawk-iter.1 | 27 | ||||
-rw-r--r-- | testcases-iter | 15 |
3 files changed, 49 insertions, 0 deletions
diff --git a/cppawk-include/iter.h b/cppawk-include/iter.h index f73fbea..fee8c1d 100644 --- a/cppawk-include/iter.h +++ b/cppawk-include/iter.h @@ -279,6 +279,13 @@ function __loop_argmin(a, arga, b, argb) #define __fini_argmin(am, arg, expr) 1 #define __step_argmin(am, arg, expr) 1 +#define __temp_counting(var, expr) +#define __init_counting(var, expr) var = 0 +#define __test_counting(var, expr) 1 +#define __prep_counting(var, expr) (expr) ? var++ : 1 +#define __fini_counting(var, expr) 1 +#define __step_counting(var, expr) 1 + #define __temp_while(expr) #define __init_while(expr) 1 #define __test_while(expr) expr diff --git a/cppawk-iter.1 b/cppawk-iter.1 index d55fb24..490c9cb 100644 --- a/cppawk-iter.1 +++ b/cppawk-iter.1 @@ -84,6 +84,7 @@ iter \- powerful, user-extensible iteration language for Awk \fI// calculating clauses\fP summing (\fIvar\fP, \fIexpr\fP) + counting (\fIvar\fP, \fIexpr\fP) maximizing (\fIvar\fP, \fIexpr\fP) minimizing (\fIvar\fP, \fIexpr\fP) argmax (\fImaxvar\fP, \fIarg\fP, \fIexpr\fP) @@ -787,6 +788,32 @@ ends up with the sum of the samples of the value of .I expr from before each iteration of the loop. +.SS Loop clause \fIcounting\fP +.bk +.B Syntax: + +.ft B + counting (\fIvar\fP, \fIexpr\fP) +.ft R + +.B Description + +The +.B counting +clause initialized +.I var +to zero. Prior to each iteration of the loop, +.I expr +is evaluated and if it yields a true value, then +.I var +is incremented. + +Thus, +.I var +ends up with a count of the number of iterations in which +.I expr +was true. + .SS Loop clauses \fIminimizing\fP and \fImaximizing\fP .bk .B Syntax: diff --git a/testcases-iter b/testcases-iter index 9ed1c25..54153d8 100644 --- a/testcases-iter +++ b/testcases-iter @@ -442,3 +442,18 @@ BEGIN { : -1 -1 +-- +21: +$cppawk ' +#include <iter.h> + +BEGIN { + loop (range (x, 1, 11), + counting (oddc, x % 2 != 0), + counting (evenc, x % 2 == 0), + counting (negativec, x < 0)) + ; + print oddc, evenc, negativec +}' +: +6 5 0 |