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 / setmerge.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  71 lines

  1. ############################################################################
  2. #
  3. #    File:     setmerge.icn
  4. #
  5. #    Subject:  Program to combine sets of text items
  6. #
  7. #    Author:   Gregg M. Townsend
  8. #
  9. #    Date:     May 31, 1994
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Setmerge combines sets of items according to the specified operators.
  18. #  Sets are read from files, one entry per line.  Operation is from left
  19. #  to right without any precedence rules.  After all operations are
  20. #  complete the resulting set is sorted and written to standard output.
  21. #  Usage:  setmerge file [[op] file]...
  22. #
  23. #  Operations:
  24. #    +  add contents to set
  25. #    -  subtract contents from set
  26. #    *  intersect contents with set
  27. #
  28. #  Note that operators must be separate command options, and that some
  29. #  shells my require some of them to be quoted.
  30. #
  31. #  Example 1: combine files, sorting and eliminating duplicates:
  32. #
  33. #    setmerge file1 + file2 + file3 + file4
  34. #
  35. #  Example 2: print lines common to three files
  36. #
  37. #    setmerge file1 '*' file2 '*' file3
  38. #
  39. #  Example 3: print lines in file1 or file2 but not in file3
  40. #
  41. #    setmerge file1 + file2 - file3
  42. #
  43. ############################################################################
  44.  
  45.  
  46. procedure main(args)
  47.    local items, a, op, f, s
  48.  
  49.    items := set()
  50.    op := "+"
  51.    every a := !args do {
  52.       if *a = 1 & any('+-*', a) then {
  53.          op := a
  54.          }
  55.       else {
  56.          f := open(a) | stop("can't open ", a)
  57.          case op of {
  58.             "+": every insert(items, !f)
  59.             "-": every delete(items, !f)
  60.             "*": {
  61.                s := set()
  62.                every insert(s, member(items, !f))
  63.                items := s
  64.                }
  65.          }
  66.       }
  67.    }
  68.    every write(!sort(items))
  69. end
  70.