diff options
-rw-r--r-- | cppawk-include/iter.h | 7 | ||||
-rw-r--r-- | cppawk-iter.1 | 35 | ||||
-rw-r--r-- | testcases-iter | 15 |
3 files changed, 57 insertions, 0 deletions
diff --git a/cppawk-include/iter.h b/cppawk-include/iter.h index fee8c1d..e2bdeac 100644 --- a/cppawk-include/iter.h +++ b/cppawk-include/iter.h @@ -319,4 +319,11 @@ function __loop_argmin(a, arga, b, argb) #define __fini_if(test, clause) __fini_ ## clause #define __step_if(test, clause) (test) && __step_ ## clause +#define __temp_records(file) __g(stream) +#define __init_records(file) __g(stream) = file +#define __test_records(file) getline < __g(stream) +#define __prep_records(file) 1 +#define __fini_records(file) __g(stream) != "-" ? close(__g(stream)) : 1 +#define __step_records(file) 1 + #endif diff --git a/cppawk-iter.1 b/cppawk-iter.1 index 490c9cb..b45cd30 100644 --- a/cppawk-iter.1 +++ b/cppawk-iter.1 @@ -77,6 +77,7 @@ iter \- powerful, user-extensible iteration language for Awk list (\fIiter\fP, \fIvar\fP, \fIlist\fP) fields (\fIvar\fP) keys (\fIkey\fP, \fIarray\fP) + records (\fIfile\fP) \fI// collect items into lists\fP collect (\fIvar\fP, \fIexpr\fP) @@ -684,6 +685,40 @@ parameter. The variable is set to each index in turn. The keys are not visited in any specific, required order. +.SS Loop clause \fIrecords\fP +.bk +.B Syntax: + +.ft B + records (\fIfile\fP) +.ft R + +.B Description: + +The +.B records +clause is based on the Awk +.B getline +operator. The +.I file +expression is evaluated once, prior to the loop, and its value is +remembered. Then prior to each iteration of the loop, a record is +read from the file using +.B getline +which has the effect of setting the record parameter +.B $0 +and the positional parameters +.BR $1 , +.BR $2 ", ..." +in the manner documented of the +.B getline +operator. + +When the loop terminates normally, the file is closed, unless +it is the string +.B \(dq-\(dq +denoting standard input. + .SH LOOP CLAUSES: COLLECTION INTO LISTS .bk .SS Loop clauses \fIcollect\fP and \fIcollect_plus\fP diff --git a/testcases-iter b/testcases-iter index 54153d8..4d00782 100644 --- a/testcases-iter +++ b/testcases-iter @@ -457,3 +457,18 @@ BEGIN { }' : 6 5 0 +-- +22: +$cppawk ' +#include <iter.h> + +BEGIN { + OFS=":" + loop (records("testdir/data")) { + $1=$1 + print + } +}' +: +a:b:c:d +e:f:g:h |