91 lines
1.9 KiB
Awk
91 lines
1.9 KiB
Awk
# 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)
|
|
{
|
|
return ((left + 0) < (right + 0))
|
|
}
|
|
|
|
# num_ge --- do a numeric greater than or equal to comparison
|
|
|
|
function num_ge(left, right)
|
|
{
|
|
return ((left + 0) >= (right + 0))
|
|
}
|
|
# do_sort --- sort the data according to `compare'
|
|
# and return it as a string
|
|
|
|
function do_sort(first, last, compare, data, i, retval)
|
|
{
|
|
delete data
|
|
for (i = 1; first <= last; first++) {
|
|
data[i] = $first
|
|
i++
|
|
}
|
|
|
|
quicksort(data, 1, i-1, compare)
|
|
|
|
retval = data[1]
|
|
for (i = 2; i in data; i++)
|
|
retval = retval " " data[i]
|
|
|
|
return retval
|
|
}
|
|
# sort --- sort the data in ascending order and return it as a string
|
|
|
|
function sort(first, last)
|
|
{
|
|
return do_sort(first, last, "num_lt")
|
|
}
|
|
|
|
# rsort --- sort the data in descending order and return it as a string
|
|
|
|
function rsort(first, last)
|
|
{
|
|
return do_sort(first, last, "num_ge")
|
|
}
|