summaryrefslogtreecommitdiff
path: root/ipl/progs/datmerge.icn
blob: 56b703ffb46cf87bf5e9ac1d9b95b5dc7c70ae10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
############################################################################
#
#	File:     datmerge.icn
#
#	Subject:  Program to merge data files
#
#	Author:   Gregg M. Townsend
#
#	Date:     November 16, 2001
#
############################################################################
#
#	This file is in the public domain.
#
############################################################################
#
#	Datmerge reads and combines arbitrary text-based data files that
#	contain whitespace-separated data.  For each data field, a single
#	value is written to standard output after applying a selected
#	operator (such as median or minimum) to the corresponding values
#	from all the input files.
#
#	Usage:  datmerge [-operator] filename...
#
#	Operators:
#	    -min or -minimum
#	    -max or -maximum
#	    -med or -median   (this is the default)
#	    -mean
#
#	Values convertible to numeric are treated as such.
#	All others are treated as strings.
#
############################################################################
#
#  Links:  numbers, strings
#
############################################################################

link numbers, strings



procedure main(args)
   local a, opr, files, lines

   if args[1][1] == '-' then {
      a := get(args)
      opr := case a of {
         "-min" | "-minimum":  minimum
         "-max" | "-maximum":  maximum
         "-med" | "-median":   median
         "-mean":              mean
         default:              stop(&progname, ": unrecognized operator: ", a)
         }
      }
   else
      opr := median

   if *args < 1 then
      stop("usage: ", &progname, " [-operator] filename...")

   files := []
   while a := get(args) do
      put(files, open(a)) | stop("cannot open ", a)

   repeat {
      lines := []
      every put(lines, read(!files))
      if *lines = 0 then break
      merge(lines, opr)
      }

end



#  merge(lines, opr) -- output the result of merging a list of lines.

procedure merge(lines, opr)
   local a, s, w, fields, ws

   fields := []
   every s := !lines do {
      put(fields, a := [])
      every w := words(s) do
         put(a, numeric(w) | w)
      }

   ws := ""
   repeat {
      a := []
      every put(a, get(!fields))
      if *a = 0 then break
      writes(ws, opr(a))
      ws := " "
      }

   write()
end



#  Operator Procedures
#
#  These procedures take a list and return a value.
#  They must always return something regardless of the data.
#  Those that involve arithmetic need to tolerate string data somehow.

procedure minimum(a)
   a := sort(a)
   return a[1]
end

procedure maximum(a)
   a := sort(a)
   return a[-1]
end

procedure mean(a)
   return (amean ! nsubset(a)) | median(a)
end

procedure median(a)
   a := sort(a)
   return a[(*a + 1) / 2]
end



#  nsubset(a) -- return subset of array a that contains numeric values

procedure nsubset(a)
   local b
   b := []
   every put(b, numeric(!a))
   if *b > 0 then
      return b
   else
      fail
end