aboutsummaryrefslogtreecommitdiffstats
path: root/awklib/eg/prog/indirectcall.awk
diff options
context:
space:
mode:
authorArnold D. Robbins <arnold@skeeve.com>2018-05-27 23:14:14 +0300
committerArnold D. Robbins <arnold@skeeve.com>2018-05-27 23:14:14 +0300
commita9df82701a087b45b4c6991fb0c20f6911c278ad (patch)
treeddc1ae7cc962003964ac211ac9dd9353fc3691fb /awklib/eg/prog/indirectcall.awk
parent94916f2066cc45507ac9605dfc897f63eb3f0ea0 (diff)
parent29f1563294ac1ab19aa252f3fd5fca94c4f88516 (diff)
downloadegawk-a9df82701a087b45b4c6991fb0c20f6911c278ad.tar.gz
egawk-a9df82701a087b45b4c6991fb0c20f6911c278ad.tar.bz2
egawk-a9df82701a087b45b4c6991fb0c20f6911c278ad.zip
Merge branch 'gawk-4.2-stable'
Diffstat (limited to 'awklib/eg/prog/indirectcall.awk')
-rw-r--r--awklib/eg/prog/indirectcall.awk45
1 files changed, 45 insertions, 0 deletions
diff --git a/awklib/eg/prog/indirectcall.awk b/awklib/eg/prog/indirectcall.awk
index 165b022a..b2b82686 100644
--- a/awklib/eg/prog/indirectcall.awk
+++ b/awklib/eg/prog/indirectcall.awk
@@ -1,3 +1,48 @@
+# indirectcall.awk --- Demonstrate indirect function calls
+#
+# Arnold Robbins, arnold@skeeve.com, Public Domain
+# January 2009
+# average --- return the average of the values in fields $first - $last
+
+function average(first, last, sum, i)
+{
+ sum = 0;
+ for (i = first; i <= last; i++)
+ sum += $i
+
+ return sum / (last - first + 1)
+}
+
+# sum --- return the sum of the values in fields $first - $last
+
+function sum(first, last, ret, i)
+{
+ ret = 0;
+ for (i = first; i <= last; i++)
+ ret += $i
+
+ return ret
+}
+# For each record, print the class name and the requested statistics
+{
+ class_name = $1
+ gsub(/_/, " ", class_name) # Replace _ with spaces
+
+ # find start
+ for (i = 1; i <= NF; i++) {
+ if ($i == "data:") {
+ start = i + 1
+ break
+ }
+ }
+
+ printf("%s:\n", class_name)
+ for (i = 2; $i != "data:"; i++) {
+ the_function = $i
+ printf("\t%s: <%s>\n", $i, @the_function(start, NF) "")
+ }
+ print ""
+}
# num_lt --- do a numeric less than comparison
function num_lt(left, right)