home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / quotword.zip / w.cmd < prev   
OS/2 REXX Batch file  |  1998-05-28  |  3KB  |  106 lines

  1. /*-w.cmd----------------------------------------*/
  2. /* Word()-like Function (handles quoted strings)*/
  3. /* Peter Flass <Flass@LBDC.Senate.State.NY.US>  */
  4. /* May, 1998                                    */
  5. /* Usage: result = W(string,word#)              */
  6. /*----------------------------------------------*/
  7.  
  8.   /* Uncomment the following line if imbedding  */
  9. /* ---
  10. W:Procedure   
  11.  ---*/
  12.   Signal On Novalue
  13.   Parse Arg str,num
  14.   word     = 0
  15.   i=1
  16.  
  17.   Do While(i<=Length(str))     
  18.      HaveWord  = ''
  19.      ThisWord  = ''
  20.      quote     = ''
  21.      c1        = ''
  22.  
  23.      /* Find Start of String */
  24.      Do While(i<=Length(str))
  25.         c1 = Substr(str,i,1)
  26.         If c1<>' ' Then Leave
  27.         i=i+1
  28.         End /* do i */
  29.      If i>Length(str) Then Return '' /*052798*/
  30.  
  31.      If c1="'" | c1='"' Then Do
  32.         quote=c1
  33.         i=i+1
  34.         if i>Length(str) Then Return ''/* Single quote only */
  35.         c1 = Substr(str,i,1)
  36.         End /* quote */
  37.  
  38.      /* Scan string */
  39.      Do Forever
  40.         i=i+1
  41.         if i>Length(str) Then Do
  42.            If c1=quote Then Do
  43.               HaveWord='Y'
  44.               Leave /* Forever */
  45.               End
  46.            If quote<>'' Then Return '' /* No closing quote */
  47.            ThisWord = ThisWord || c1
  48.            HaveWord='Y'
  49.            End /* i>Length(str) */
  50.         If HaveWord<>'' Then Leave /* Forever */
  51.         c2 = Substr(str,i,1) 
  52.  
  53.         /* Not a quoted string */
  54.         If quote='' Then Do
  55.            If c2=' ' Then Do
  56.               ThisWord = ThisWord || c1
  57.               HaveWord='Y'
  58.               End /* c2=' ' */
  59.            Else Do
  60.               ThisWord = ThisWord || c1
  61.               c1=c2
  62.               Iterate
  63.               End /* else */
  64.            End /* quote='' */ 
  65.  
  66.         /* Quoted string */
  67.         Else Do
  68.            Select
  69.               /* Quote-Quote -> Quote */
  70.               When c1=quote & c2=quote Then Do
  71.                  ThisWord = ThisWord || c1
  72.                  i=i+1
  73.                  If i>Length(str) Then Return '' /* ends with '' */
  74.                  c1=Substr(str,i,1)
  75.                  Iterate
  76.                  End /* quote-quote */
  77.               /* Quote-x: end of string */
  78.               When c1=quote & c2<>quote Then Do 
  79.                  HaveWord='Y'
  80.                  End
  81.               /* Quote-<EOS> */
  82.               When c1=quote & i>=Length(str) Then Do
  83.                  HaveWord='Y'
  84.                  End 
  85.               /* x-Anything */
  86.               Otherwise Do
  87.                  ThisWord = ThisWord || c1
  88.                  c1=c2
  89.                  Iterate
  90.                  End /* otherwise */
  91.               End /* Select */
  92.            End /* Quoted string */
  93.  
  94.          If HaveWord<>'' Then Leave /* scan */
  95.  
  96.          End /* scan */
  97.  
  98.       word=word+1
  99.       If word=num Then Return ThisWord
  100.  
  101.       End /* Do While(i) */
  102.  
  103.    Return ''
  104.  
  105. /*------------ End of 'W' --------------*/
  106.