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

  1. ############################################################################
  2. #
  3. #    File:     countlst.icn
  4. #
  5. #    Subject:  Program to count items in a list
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #     This program counts the number times each line of input occurs and
  18. #  writes a summary.
  19. #
  20. #     With no option, the output is sorted first by decreasing count and within
  21. #  each count, alphabetically. With the option -a, the output is sorted
  22. #  alphabetically.
  23. #
  24. #  The option -t prints a total at the end.
  25. #
  26. ############################################################################
  27. #
  28. #  Links: adlutils, options
  29. #
  30. ############################################################################
  31.  
  32. link adlutils, options
  33.  
  34. procedure main(args)
  35.    local line_count, counter, lines, opts, sort_method, line, total, count
  36.  
  37.    line_count := table(0)        # counts for each line
  38.    counter := table()            # lists of lines for each count
  39.    total := 0                # total number of lines
  40.  
  41.    opts := options(args,"at")
  42.    sort_method := opts["a"]
  43.  
  44.    while line_count[read()] +:= 1 do
  45.       total +:= 1
  46.        
  47.    if \sort_method then {        # alphabetical sort
  48.       line_count := sort(line_count,3)
  49.       while write(get(line_count),"\t",get(line_count))
  50.       }
  51.    else {                 # numerical sort, then alpha
  52.       line_count := sort(line_count,4)
  53.    
  54.       while count := pull(line_count) do {
  55.          /counter[count] := []
  56.          put(counter[count],pull(line_count))
  57.          }
  58.    
  59.       counter := sort(counter,3)
  60.    
  61.       while lines := sort(pull(counter)) do {
  62.          count := pull(counter)
  63.          every write(!lines,"\t",count)
  64.          }
  65.      }
  66.  
  67.    if \opts["t"] then write("\ntotal\t",total)
  68.  
  69. end
  70.