Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
GETWORDNUM() Substitute
Message
From
16/08/2003 10:33:39
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
GETWORDNUM() Substitute
Miscellaneous
Thread ID:
00820749
Message ID:
00820749
Views:
48
Here is a substitute for GETWORDNUM() which I call GETWORD().
It can be useful for those developers running version of VFP
less than 7. It can also be useful for those wishing to "tweak"
the operation of GETWORDNUM().
This version of GETWORD() seems to work OK. The only quirk
that I have found occurs when you ask for a word number higher
than the last word in the string, it will return the last word
again. Maybe someone here will want to play with it and fix it?
Anyway, here it is for you use. Enjoy!
Terry Harris

==================================================================
*** The calling routine:

STORE "Marietta, GA  30066" TO my_string

*** Display word# 3 in the string which is "30066". ***
STORE 3 TO next_word
STORE GETWORD(my_string, next_word, " ") TO new_word
? next_word
? new_word
WAIT

* A "4" causes word "3" to be output again. (Quirk.)
STORE 4 TO next_word
STORE GETWORD(my_string,next_word," ") TO new_word
? next_word
? new_word
WAIT
RETURN
==================================================================
FUNCTION GETWORD
LPARAMETERS orig_str, desired_word_num, delimiter_char

LOCAL bypass, comparemv, current_word, start_point, stop_point, tempmv

STORE ALLTRIM(orig_str) TO orig_str
STORE "N" TO bypass
IF LEN(orig_str) = 0
   STORE "Y" TO bypass
ENDIF
* Add the delimiter to the end of the original string so 
*     the last word can be gotten.
STORE orig_str + delimiter_char TO orig_str


STORE 1 TO start_point
STORE 0 TO current_word
STORE 1 TO countermv
STORE "" TO tempmv
STORE LEN(orig_str) + 1 TO stop_point

DO WHILE (current_word < desired_word_num) AND  ;
      countermv < stop_point AND bypass = "N"   && DO WHILE #1

   IF start_point > stop_point - 1
      STORE stop_point - 1 TO start_point
   ENDIF
   STORE "" TO comparemv
   STORE "" TO tempmv

   *-------------------------------------------------------
   * Get a word from the original string (orig_str).
   SET EXACT ON
   DO WHILE countermv < stop_point  && DO WHILE #2
      * This will loop until the counter gets up to the "stop_point" 
      *      or an EXIT is reached.
      * The "stop_point" is "one count past" the end of the string.
      STORE SUBSTR(orig_str, countermv, 1) TO comparemv
      IF comparemv = delimiter_char
         * The "countermv - start_point" causes the delimiter not 
         *     to be included in the output.
         STORE SUBSTR(orig_str, start_point, countermv - start_point ) ;
               TO tempmv
         EXIT     &&  <--- exit loop
      ENDIF
      STORE countermv + 1 TO countermv
   ENDDO  && DO WHILE #2
   SET EXACT OFF
   *-------------------------------------------------------


 
   * Set the new start_point for the next word.
   STORE countermv TO start_point

   * Point to the next character (beyond the delimiter) in the string.
   STORE countermv + 1 TO countermv

   IF delimiter_char = tempmv
      * There were two or more delimiters in a row.  
      * So, do not count the extra delimiter as a word.
   ELSE
      * Point to the next word in the string.
      STORE current_word + 1 TO current_word
   ENDIF


ENDDO  && DO WHILE #1

* Remove leading and trailing spaces.
STORE ALLTRIM(tempmv) TO tempmv

RETURN tempmv
Next
Reply
Map
View

Click here to load this message in the networking platform