home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / v / vsnbl220.zip / WORDSIZE.SNO < prev   
Text File  |  1991-02-14  |  2KB  |  58 lines

  1. *    WORDSIZE.SNO
  2. *
  3. *    Program to read a file and display the number of words of
  4. *    various word lengths.  To make the program more interesting,
  5. *    we shall only consider word lengths between 3 and 9.  This allows
  6. *    us to demonstrate the use of an array with subscripts offset from
  7. *    1, as well as array failure.
  8. *
  9. *    The file being scanned is read from standard input.  For example,
  10. *    to scan the file TEXT.IN, type:
  11. *
  12. *        SNOBOL4 WORDSIZE <TEXT.IN
  13. *
  14. *    Trim trailing blanks from input
  15. *
  16.     &TRIM    =    1
  17.  
  18. *    Define pattern for words.  A word consists of upper- and lower-case
  19. *    letters, apostrosphe and hyphen.
  20. *
  21.     WORDPAT    =    BREAK(&LCASE &UCASE) SPAN(&LCASE &UCASE "'-") . WORD
  22.  
  23. *    Define the array to hold the word counts.  Valid subscripts must be
  24. *    in the range 3 through 9; all others will cause the array reference
  25. *    to fail.  Array elements are initialized to zero instead of the normal
  26. *    default, which is the null string.  This causes a zero to be produced
  27. *    in the printed output if a particular array entry is never incremented.
  28. *
  29.     COUNT    =    ARRAY('3:9',0)
  30.  
  31. *    Read a line from the input file.  Fail if end-of-file.
  32. *
  33. READ    LINE    =    INPUT                :F(DONE)
  34.  
  35. *    Find the next word in LINE, and remove it to WORD.  Fail when
  36. *    no more words remain in the line.
  37. *
  38. NEXTW    LINE WORDPAT =                    :F(READ)
  39.  
  40. *    Increment the appropriate array element for words of this
  41. *    size.  The statement quietly fails if the size is outside
  42. *    the range 3 through 9.
  43. *
  44.     COUNT<SIZE(WORD)> = COUNT<SIZE(WORD)> + 1    :(NEXTW)
  45.  
  46. *    Upon end of file, print the values in the array.  Print heading first.
  47. *
  48. DONE    OUTPUT    =    "WORD LENGTH     NUMBER OF OCCURRENCES"
  49.     I    =    2
  50.  
  51. *    Index through array starting at element 3.  When we reach element
  52. *    10, the array reference fails, and we fall through to END.
  53. *
  54. PRINT    I    =    I + 1
  55.     OUTPUT    =    LPAD(I,5) LPAD(COUNT<I>,20)    :S(PRINT)
  56.  
  57. END
  58.