home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / datmerge.icn < prev    next >
Text File  |  2001-11-16  |  3KB  |  142 lines

  1. ############################################################################
  2. #
  3. #    File:     datmerge.icn
  4. #
  5. #    Subject:  Program to merge data files
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     November 16, 2001
  10. #
  11. ############################################################################
  12. #
  13. #    This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #    Datmerge reads and combines arbitrary text-based data files that
  18. #    contain whitespace-separated data.  For each data field, a single
  19. #    value is written to standard output after applying a selected
  20. #    operator (such as median or minimum) to the corresponding values
  21. #    from all the input files.
  22. #
  23. #    Usage:  datmerge [-operator] filename...
  24. #
  25. #    Operators:
  26. #        -min or -minimum
  27. #        -max or -maximum
  28. #        -med or -median   (this is the default)
  29. #        -mean
  30. #
  31. #    Values convertible to numeric are treated as such.
  32. #    All others are treated as strings.
  33. #
  34. ############################################################################
  35. #
  36. #  Links:  numbers, strings
  37. #
  38. ############################################################################
  39.  
  40. link numbers, strings
  41.  
  42.  
  43.  
  44. procedure main(args)
  45.    local a, opr, files, lines
  46.  
  47.    if args[1][1] == '-' then {
  48.       a := get(args)
  49.       opr := case a of {
  50.          "-min" | "-minimum":  minimum
  51.          "-max" | "-maximum":  maximum
  52.          "-med" | "-median":   median
  53.          "-mean":              mean
  54.          default:              stop(&progname, ": unrecognized operator: ", a)
  55.          }
  56.       }
  57.    else
  58.       opr := median
  59.  
  60.    if *args < 1 then
  61.       stop("usage: ", &progname, " [-operator] filename...")
  62.  
  63.    files := []
  64.    while a := get(args) do
  65.       put(files, open(a)) | stop("cannot open ", a)
  66.  
  67.    repeat {
  68.       lines := []
  69.       every put(lines, read(!files))
  70.       if *lines = 0 then break
  71.       merge(lines, opr)
  72.       }
  73.  
  74. end
  75.  
  76.  
  77.  
  78. #  merge(lines, opr) -- output the result of merging a list of lines.
  79.  
  80. procedure merge(lines, opr)
  81.    local a, s, w, fields, ws
  82.  
  83.    fields := []
  84.    every s := !lines do {
  85.       put(fields, a := [])
  86.       every w := words(s) do
  87.          put(a, numeric(w) | w)
  88.       }
  89.  
  90.    ws := ""
  91.    repeat {
  92.       a := []
  93.       every put(a, get(!fields))
  94.       if *a = 0 then break
  95.       writes(ws, opr(a))
  96.       ws := " "
  97.       }
  98.  
  99.    write()
  100. end
  101.  
  102.  
  103.  
  104. #  Operator Procedures
  105. #
  106. #  These procedures take a list and return a value.
  107. #  They must always return something regardless of the data.
  108. #  Those that involve arithmetic need to tolerate string data somehow.
  109.  
  110. procedure minimum(a)
  111.    a := sort(a)
  112.    return a[1]
  113. end
  114.  
  115. procedure maximum(a)
  116.    a := sort(a)
  117.    return a[-1]
  118. end
  119.  
  120. procedure mean(a)
  121.    return (amean ! nsubset(a)) | median(a)
  122. end
  123.  
  124. procedure median(a)
  125.    a := sort(a)
  126.    return a[(*a + 1) / 2]
  127. end
  128.  
  129.  
  130.  
  131. #  nsubset(a) -- return subset of array a that contains numeric values
  132.  
  133. procedure nsubset(a)
  134.    local b
  135.    b := []
  136.    every put(b, numeric(!a))
  137.    if *b > 0 then
  138.       return b
  139.    else
  140.       fail
  141. end
  142.