home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1941 < prev    next >
Internet Message Format  |  1990-12-28  |  5KB

  1. From: manny@wet.UUCP (manny juan)
  2. Newsgroups: alt.sources
  3. Subject: Here's an Acrostic Maker in awk!
  4. Message-ID: <1620@wet.UUCP>
  5. Date: 12 Oct 90 05:29:29 GMT
  6.  
  7.  
  8. #
  9. # ACROSTIC MAKER
  10. #
  11. # by Manny Juan
  12. #    61 Oakmont Drive
  13. #    Daly City, CA 94015
  14. #
  15. #    email: manny@wet.UUCP
  16. #
  17. #    October 10, 1990
  18. #
  19. # Instructions:
  20. #
  21. #  Edit a file similar to the example below (each card starts at column 1).
  22. #  You may cut the segment below to create SAMPLE.PZL after deleting the
  23. #  first two characters of each card.
  24. #
  25. # 12345678901234567890...
  26. #---- SAMPLE.PZL --- cut here
  27. # quote without reagan there would have been
  28. # quote no gorbachev
  29. # quote
  30. # word enough : sufficient
  31. # word throw : cast
  32. # word broad : wide
  33. # word watch : timepiece
  34. # word elevate : what otis machines do
  35. # word avenue : street
  36. # word neighbor : person next door
  37. #---- end of sample.pzl
  38. #
  39. #  Enter the quotation using keyword "quote" WITHOUT punctuations
  40. #  using as many "quote" cards as needed.  Supply final "quote" by
  41. #  itself to terminate the quotation.
  42. #
  43. #  Cards with keyword "word" define the clue words.  The definition
  44. #  portion may be delayed until the last minute when all letters in
  45. #  the quotation have been used.  Start with a few clue words at the
  46. #  beginning and execute the program by typing:
  47. #
  48. #     awk acrostic sample.pzl
  49. #
  50. #  Repeat the editing and checking as long as more letters remain in
  51. #  the letter pool.  During these runs, output will be displayed on the
  52. #  screen.
  53. #
  54. #  When all letters in the original quote have been used, re-edit the
  55. #  puzzle file and supply the word definitions by appending a colon (:)
  56. #  and the definition to each clue word.
  57. #
  58. #  Print the puzzle by typing:
  59. #
  60. #     awk acrostic trace=off sample.pzl >output.dvc
  61. #
  62. #  Note: The printer width is assumed to be 132.  To adjust, for example
  63. #  to 80, type:
  64. #
  65. #     awk acrostic ls=80 trace=off sample.pzl >output.dvc
  66. #
  67. BEGIN {
  68.   if(ARGC<2) {
  69.     err=1
  70.     print("Usage: awk acrostic [ls=nn] [trace=off] input.pzl [>output]")
  71.     exit
  72.   }
  73.   ls=132 # default line size
  74.   uppers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  75.   lowers = "abcdefghijklmnopqrstuvwxyz"
  76.   alf="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  77.   wn=0 #word counter
  78.   qstr=""
  79.   qlst=""
  80.   err=1 # clear this when final "quote" is read
  81.   }
  82. #
  83. {
  84.   for(i=1;i<=26;i++){
  85.     gsub(substr(lowers,i,1),substr(uppers,i,1))
  86.   }
  87. }
  88. $1=="QUOTE" {
  89.   for(i=2;i<=NF;i++) {
  90.     qstr=qstr " " $i
  91.     qlst=qlst $i
  92.   }
  93. }
  94. $1=="QUOTE" && $2=="" {
  95.   for(i=1;i<=length(qlst);i++) {
  96.     l=substr(qlst,i,1)
  97.     qcount[l]++
  98.     qllist[l]=qllist[l] " " i
  99.   }
  100.   err=0 # to clear err set in BEGIN
  101.  
  102.   if(trace!="off")
  103.     print("pool=",ltrpool())
  104. }
  105. $1=="WORD" {
  106.   wn++
  107.   word=$2
  108.   wdlist[wn]=$2
  109.   wcount[wn]=length(word)
  110.   wclues[wn]=$0
  111.   if(trace!="off")
  112.     print("\nword=", word)
  113.   for(i=1;i<=length(word);i++){
  114.     c=substr(word,i,1)
  115.     if(qcount[c]==0){
  116.       print"cannot find word " word " in pool"
  117.       exit
  118.     }
  119.     else {
  120.       split(qllist[c], ql, " ")
  121.       wllist[wn]=wllist[wn] " " ql[qcount[c]]
  122.       # strip off last entry
  123.       qlwork=ql[qcount[c]]
  124.       #update solution string
  125.       qsol[qlwork]=substr(alf,wn,1)
  126.       #remove from list
  127.       match(qllist[c], qlwork)
  128.       qllist[c]=substr(qllist[c],1,RSTART-1)
  129.       qcount[c]--
  130.     }
  131.   }
  132.   if(trace!="off")
  133.     print("pool=",ltrpool())
  134. }
  135. END {
  136.   poolsize=0
  137.   for(i=1;i<=26;i++){
  138.     l=substr(alf,i,1)
  139.     if(qcount[l]>0){
  140.       poolsize=poolsize+qcount[l]
  141.     }
  142.   }
  143.   if(err) {
  144.     exit
  145.   }
  146.   if(poolsize!=0) {
  147.     print("\nletters still remain in the pool")
  148.     exit
  149.   }
  150.  
  151. #print puzzle
  152.   mid=int(ls/2)
  153.   print("CLUES:")
  154.   for(i=1;i<=wn;i++){
  155.     # locate ":" and get clue from string following
  156.     match(wclues[i],":")
  157.     if(RSTART==0)
  158.       clue=wdlist[i]
  159.     else
  160.       clue=substr(wclues[i],RSTART+1)
  161.     printf("\n\n%1s. %-" mid "s",substr(alf,i,1), clue)
  162.     for(j=1;j<=wcount[i];j++) {
  163.       printf("%-4s","___ ")
  164.     }
  165.     printf("\n  %" mid "s"," ")
  166.     split(wllist[i],w)
  167.     for(j=1;j<=wcount[i];j++) {
  168.       printf("%4d",w[j])
  169.     }
  170.   }
  171.   printf("\n")
  172.  
  173. #print matrix
  174.   cols=int(ls/5)
  175.   rows=int(length(qlst)/cols)+1
  176.   printf("\n\n")
  177. # get beyond initial blank in qstr
  178.   qstr=substr(qstr,2)
  179.   n=0
  180.   for(r=1;r<=rows;r++){
  181.     s1=""
  182.     s2=""
  183.     s3=""
  184.     for(i=1;i<=cols;i++){
  185.       s1=s1 sprintf("%-5s","+----")
  186.     }
  187.     s1=s1 "+"
  188.     print s1
  189.     for(i=1;i<=cols;i++){
  190.       ch=substr(qstr,1,1)
  191.       qstr=substr(qstr,2) " "
  192.       if(ch==" ") {
  193.         s2=s2 "|@@@@"
  194.         s3=s3 "|@@@@"
  195.       }
  196.       else {
  197.         n++
  198.         s2=s2 sprintf("|%s%-3d",qsol[n],n)
  199.         s3=s3 "|    "
  200.       }
  201.     }
  202.     s2=s2 "|"
  203.     s3=s3 "|"
  204.     print s2
  205.     print s3
  206.     print s3
  207.   }
  208.   print s1
  209. }
  210.  
  211. function rptch(c,n,t) { # return string of n c's
  212.   while(n-- >0)
  213.     t=t c
  214.   return t
  215. }
  216.  
  217. function ltrpool(x,pool) {
  218.   pool=""
  219.   for(i=1;i<=26;i++){
  220.     l=substr(alf,i,1)
  221.     if(qcount[l]>0){
  222.       pool=pool rptch(l,qcount[l])
  223.     }
  224.   }
  225.   return(pool)
  226. }
  227.