1: $cppawk ' #include BEGIN { split("a b c d e f", a) doarray (i, v, a) print i, v }' : 1 a 2 b 3 c 4 d 5 e 6 f -- 2: $cppawk ' #include BEGIN { doarray (i, v, a) print i, v }' : -- 3: $cppawk ' #include BEGIN { $0 = "a b c d e" dofields (i, v) print i, v }' : 1 a 2 b 3 c 4 d 5 e -- 4: $cppawk ' #include BEGIN { dostring (i, c, 123.0) { print i, c } }' : 1 1 2 2 3 3 -- 5: $cppawk ' #include BEGIN { dostring (i, c, ""); }' : -- 6: $cppawk ' #include BEGIN { $0 = "o p q r s t u v w x y z" # set fields split("! @ # $ % ^ & * (", arr) # init array loop (range(i, 1, 9), from(x, 5), from_step(y, 100, 5), str(j, ch, "abcdefghijklmn"), fields(f), keys(k, arr)) { print i, x, y, ch, f, k, arr[k] } }' : 1 5 100 a o 1 ! 2 6 105 b p 2 @ 3 7 110 c q 3 # 4 8 115 d r 4 $ 5 9 120 e s 5 % 6 10 125 f t 6 ^ 7 11 130 g u 7 & 8 12 135 h v 8 * 9 13 140 i w 9 ( -- 7: $cppawk ' #include BEGIN { $0 = "a b c" # set fields split("X Y", arr) # init array loop_nest (fields(f), keys(k, arr), range(i, 1, 3)) { print f, arr[k], i } }' : a X 1 a X 2 a X 3 a Y 1 a Y 2 a Y 3 b X 1 b X 2 b X 3 b Y 1 b Y 2 b Y 3 c X 1 c X 2 c X 3 c Y 1 c Y 2 c Y 3 -- 8: $cppawk ' #include #define __init_let(var, expr) 1 #define __test_let(var, expr) (var = (expr)) #define __prep_let(var, expr) 1 #define __fini_let(var, expr) 1 #define __step_let(var, expr) 0 BEGIN { loop (range(i, 1, 10), range_step(j, 1, 10, 2), let(ch, substr("abcdefghijklmn", i, j))) { print i, j, ch } } ' : 1 1 a 2 3 bcd 3 5 cdefg 4 7 defghij 5 9 efghijklm -- 9: $cppawk ' #include #define __init_first_then_until(var, first, then, until) (var = (first)) #define __test_first_then_until(var, first, then, until) (!(until)) #define __prep_first_then_until(var, first, then, until) 1 #define __fini_first_then_until(var, first, then, until) 1 #define __step_first_then_until(var, first, then, until) (var = (then)) BEGIN { loop (first_then_until(i, 1, i * 2, i >= 60), first_then_until(s, "x", s s, 0)) { print i, s } }' : 1 x 2 xx 4 xxxx 8 xxxxxxxx 16 xxxxxxxxxxxxxxxx 32 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -- 10: $cppawk ' #include #include BEGIN { $0 = "a b c" # set fields split("X Y", arr) # init array bags (b1, b2) { loop_nest (parallel(fields(f), str(i, ch, "IJK")), keys(ky, arr), parallel(range(j, 1, 3), from(k, 100))) { print f, ch, arr[ky], j, k bag(b1, j) } } print sexp(b1) }' : a I X 1 100 a I X 2 101 a I X 3 102 a I Y 1 100 a I Y 2 101 a I Y 3 102 b J X 1 100 b J X 2 101 b J X 3 102 b J Y 1 100 b J Y 2 101 b J Y 3 102 c K X 1 100 c K X 2 101 c K X 3 102 c K Y 1 100 c K Y 2 101 c K Y 3 102 (1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3) -- 11: $cppawk ' #include BEGIN { loop (range(i, 1, 10), summing(s, i)) { print s } print "sum =", s }' : 1 3 6 10 15 21 28 36 45 55 sum = 55 -- 12: $cppawk ' #include BEGIN { loop (from_step (vel, 5, -1), from_step (pos, 0, vel), while (pos >= 0), maximizing (maxpos, pos)) { print pos } print "maxpos =", maxpos }' : 0 4 7 9 10 10 9 7 4 0 maxpos = 10 -- 13: $cppawk ' #include BEGIN { loop (from_step (vel, 5, -1), from_step (pos, 0, vel), until (vel <= 0), maximizing (maxpos, pos)) { print pos } print "maxpos =", maxpos }' : 0 4 7 9 10 maxpos = 10 -- 14: $cppawk ' #include BEGIN { loop (range_step (x, 0, 3.14159, 0.001), argmax (mx, x, sin(x) * cos(x)), argmin (mi, x, sin(x) * cos(x))) ; // empty print "max x =", mx print "min x =", mi }' : max x = 0.785 min x = 2.356 -- 15: $cppawk ' #include #include BEGIN { loop (list(tail, item, list(1, 2, 3, 0, 5, -3, 7)), minimizing(min, item)) { } print "min =", min }' : min = -3 -- 16: $cppawk ' #include #include function index_from(str, ch, start, sstr, pos) { sstr = substr(str, start) pos = index(sstr, ch) return pos ? pos + start - 1 : 0 } function sscl(string, char) { loop (first_then (start, 1, end + 1), for_var (end, index_from(string, char, start)), collect_plus (out, end ? substr(string, start, end - start) : substr(string, start)), while (end)) { } return out } BEGIN { print sexp(sscl(",", ",")) print sexp(sscl("a,", ",")) print sexp(sscl(",a", ",")) print sexp(sscl("a,b,c", ",")) }' : (nil nil) ("a" nil) (nil "a") ("a" "b" "c") -- 17: $cppawk ' #include #include BEGIN { loop (range (i, 1, 10), if (i % 2 == 0, collect(l, i))) { } print sexp(l) }' : (2 4 6 8 10) -- 18: $cppawk ' #include BEGIN { loop (range (i, 1, 10), if (i % 2 == 1, from(j, 1))) { print i, j } }' : 1 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 -- 19: $cppawk ' #include BEGIN { loop (while (0), argmax (mx, x, sin(x)), argmin (mi, x, sin(x))) ; // empty print mx == "" && mx != 0 print mi == "" && mi != 0 }' : 1 1 -- 20: $cppawk ' #include BEGIN { loop (range (x, -1, -1), argmax (mx, x, x), argmin (mi, x, x)) ; // empty print mx print mi }' : -1 -1 -- 21: $cppawk ' #include 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 -- 22: $cppawk ' #include BEGIN { OFS=":" loop (records("testdir/data")) { $1=$1 print } }' : a:b:c:d e:f:g:h