4.3BSD/usr/contrib/icon/src/cmd/tablw.icn

#	TABLW(1)
#
#	Tabulate words
#
#	Ralph E. Griswold
#
#	Last modified 8/11/84
#

global limit, icase

procedure main(x)
   local wcount, unique, order, i, s, a, pair, lwidth, rwidth, max
   limit := 0				# lower limit on usage to list
   unique := 0				# switch to list unique usage only
   order := 1				# alphabetical ordering switch
   i := 0
   while i < *x do {
      s := x[i +:= 1] | break
      case s of {
         "-a":  order := 1
         "-i":  icase := 1
         "-n":  order := 2
         "-s":  limit := (0 < integer(x[i +:= 1])) | Usage()
         "-u":  unique := 1
         default:  Usage()
         }
      }
   wcount := table(0)			# table of words
   every wcount[words()] +:= 1
   a := sort(wcount,order)
   if unique = 1 then {
      every pair := !a do
         if pair[2] = 1 then write(pair[1])
      }
   else {
      max := 0
      every max <:= *((!a)[1])
      lwidth := max + 3
      rwidth := 0
      every rwidth <:= *((!a)[2])
      every pair := !a do
         write(left(pair[1],lwidth),right(pair[2],rwidth))
      }
end

#  generate words
#
procedure words()
   local wchar, line, word
   wchar := &lcase ++ &ucase
   while line := read() do {
      if \icase then line := map(line)
      line ? while tab(upto(wchar)) do {
         word := tab(many(wchar)) || ((tab(any('-\'')) || tab(many(wchar))) | "")
         if *word > limit then suspend word
         }
      }
end

procedure Usage()
   stop("usage:  tablw [-a -i -n -s n -u]")
end