commit e04daa7f6bbcd9b0d0086a00b63cd8a6b8679303
parent 3ce424494e0ed622fc2617efbd02cbf61695c3ec
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Mon, 16 Sep 2013 18:47:56 +0200
looping ang getter function
Diffstat:
| M | data.awk | | | 66 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 58 insertions(+), 8 deletions(-)
diff --git a/data.awk b/data.awk
@@ -6,7 +6,8 @@ BEGIN {
# 0 read normal variables
# 1 seen start of field list, read titles
# 2 reading list values
- ident_re = /[a-zA-Z_][a-zA-Z0-9_]*/
+ # ident_re = /[a-zA-Z_][a-zA-Z0-9_]*/
+ ident_re = "[a-zA-Z_][a-zA-Z0-9_]*"
}
$0 == "]" && state == 2 {
@@ -23,32 +24,81 @@ state == 2 {
state == 1 {
V[varname,"fields"] = $0
list_n = 1
+ state = 2
next
}
-/[a-zA-Z_][a-zA-Z0-9_]*=\[$/ {
+/^[a-zA-Z_][a-zA-Z0-9_]*=\[$/ {
# start of field list
state = 1
- m = match(ident_re)
+ m = match($0, ident_re)
varname = substr($0, m, RLENGTH)
next
}
-/[a-zA-Z_][a-zA-Z0-9_]*=/ {
- m = match(ident_re)
+/^[a-zA-Z_][a-zA-Z0-9_]*=/ {
+ m = match($0, ident_re)
varname = substr($0, m, RLENGTH)
V[varname] = substr($0, m+1+RLENGTH)
+ printf "got scalar: %s â %s (%d)\n", varname, V[varname], m
next
}
-/\t/ {
+/^\t/ {
V[varname] = V[varname] "\n" substr($0, 1)
+ next
+}
+
+{
+ # TODO posix compliance
+ print "unparseable line: " $0 >"/dev/stderr"
+ exit 1
}
-# TODO error
+function loop_start(varname, prefix, depth) {
+ depth = ++loop_stack["depth"]
+ loop_stack[depth,"row"] = 0
+ loop_stack[depth,"var"] = varname
+ loop_stack[depth,"pre"] = prefix
+ return depth
+}
+
+function loop_iter(depth) {
+ loop_stack[depth,"row"]++
+ return (loop_stack[depth,"var"], loop_stack[depth,"row"]) in V
+}
+
+function loop_end() {
+ delete loop_stack[loop_stack["depth"], "row"]
+ delete loop_stack[loop_stack["depth"], "var"]
+ delete loop_stack[loop_stack["depth"], "pre"]
+ loop_stack["depth"]--
+}
+
+function get(varname, i, n, names, values) {
+ for(i=loop_stack["depth"]; i>0; i--) {
+ split(V[loop_stack[i, "var"], "fields"], names)
+ for(n in names) {
+ if(varname == (loop_stack[i, "pre"] names[n])) {
+ split(V[loop_stack[i, "var"], loop_stack[i, "row"]], values)
+ return values[n]
+ }
+ }
+ }
+ return V[varname]
+}
+# just testing it out
END {
for(key in V) {
- printf("s\ts\n", key, V[key])
+ printf("%sâ\tâ%sâ\n", key, V[key])
}
+ print "--------------------"
+ print get("foo")
+ for(d1=loop_start("spam"); loop_iter(d1);) {
+ print get("name") "-" get("value")
+ for(d2=loop_start("spam", "i_"); loop_iter(d2);) {
+ print get("eggs") + get("i_eggs")
+ }
+ } loop_end()
}