Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Ken Murphy
  Where is Ken Murphy?
 Springhill
 Canada
 Ken Murphy
 To: Nathan Davies
  Where is Nathan Davies?
 Newcastle
 United Kingdom
 Nathan Davies
 Tags
Subject: RE: For Exiting early
Thread ID: 75443 Message ID: 79065 # Views: 1 # Ratings: 0
Version: Visual FoxPro 6 Category: General VFP Topics
Date: Wednesday, October 12, 2005 4:08:05 PM         
   


As cString increases in size with the addition of spaces, what happens to LEN(cString) in the FOR? It doesn't change! I don't know if this is by design or if this is a bug. I ran into this myself a while ago. One would think that each time it loops through the "FOR" statement it should evaluate LEN(cString) but it doesn't. I think that VFP first evaluates LEN(cString) and stores this value to a local variable. It then exits when i > this local variable. In a similar situation, I ended up using a DO WHILE !llDone construct.

Perhaps we could have one of our resident VIP's check with VFP on this. If it is by design, so be it, but if it is a bug, lets get it fixed.

Ken

> I have a function that takes in one string and adds spaces around & and - and before capital letters. It is very simple and should work just fine. However, the for loop in it is exiting early. I have included the code here for you to examine.
>
> Any help or suggestions on this will be most appreciated
>
> FUNCTION AddSpaces
> PARAMETERS cString
> 
>   FOR i = 1 TO LEN(cString)
>    IF (BETWEEN(ASC(SUBSTR(cString, i, 1)), 65, 90) AND i > 1 AND ;
>      ASC(SUBSTR(cString, i-1, 1)) <> 32 ) ;
>      OR ((ASC(SUBSTR(cString, i, 1)) = 38 OR ASC(SUBSTR(cString, i, 1)) = 45) AND ASC(SUBSTR(cString, i-1, 1)) <> 32)THEN
>     *- this is an upper case letter, not preceeded by a space already or & not preceeded by a space
>     cString = STUFF(cString, i, 0, " ")
>    ENDIF && end of IF (between(....
>   ENDFOR && end of FOR i = 1 TO LEN(cString)
>  
>  RETURN cString
> ENDFUNC
> 

> When calling this with the string "MrsJingChen" the output is "Mrs Jing Chen", however if it is called with "MrsJingChen&MissHillaryBanks" the output is "Mrs Jing Chen & MissHillaryBanks" So you see the problem.
>
> I have stepped through the code and can see that for some reason it is exiting the FOR loop early, about 6 iterations early.
>
> Many thanks
>
> Nathan Davies


ENTIRE THREAD

For Exiting early Posted by Nathan Davies @ 8/24/2005 5:13:30 AM
RE: For Exiting early Posted by Barbara Peisch @ 8/24/2005 6:58:11 AM
RE: For Exiting early Posted by Nathan Davies @ 8/24/2005 10:37:08 AM
RE: For Exiting early Posted by Boudewijn Lutgerink @ 8/25/2005 11:18:06 AM
RE: For Exiting early Posted by dan delpuerto @ 10/12/2005 3:33:11 PM
RE: For Exiting early Posted by Nathan Davies @ 10/12/2005 6:02:01 PM
RE: For Exiting early Posted by dan delpuerto @ 10/13/2005 11:56:36 PM
RE: For Exiting early Posted by Ken Murphy @ 10/12/2005 4:08:05 PM
RE: For Exiting early Posted by Dennis Longfellow @ 10/12/2005 5:14:19 PM