Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Remove excessive spaces in string
Message
From
27/07/2009 13:39:24
 
 
To
25/07/2009 13:53:13
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01414370
Message ID:
01414813
Views:
47
>You can not apply a rule based on the 2^power to find
>the maximum reduced string with a default number of substitutions.
>
>Reduce_Successive fail on string 456718+1
>
>
>CLEAR
>? 2^20,LEN(Reduce_Successive(SPACE(456718+1)))
>
>FUNCTION Reduce_Successive
>LPARAMETERS lcString
>
>	lcString = STRTRAN( lcString, [                ], [ ] )
>	lcString = STRTRAN( lcString, [                ], [ ] )
>	lcString = STRTRAN( lcString, [        ], [ ] )
>	lcString = STRTRAN( lcString, [        ], [ ] )
>	lcString = STRTRAN( lcString, [    ], [ ] )
>	lcString = STRTRAN( lcString, [    ], [ ] )
>	lcString = STRTRAN( lcString, [  ], [ ] )
>	lcString = STRTRAN( lcString, [  ], [ ] )
>
>RETURN m.lcString
>
I ran two brute-force tests:

- one with the top two lines of the Reduce_Successive function commented out (nominally "good" for up to 2^12, or 4,096 spaces)

- the full function, as shown above (nominally "good" for up to 2^20, or 1,048,576 spaces)

As you point out, the function as shown above has failures. The first failure was at 456,719 spaces. In that test, there were 295,929 failures (out of 1,048,576 tests).

However, in both tests the length of the result string for the failures is always exactly 2. There are no failures with longer result strings. So, it's simple to modify Reduce_Successive() to work properly in all cases by adding one extra STRTRAN() call:
FUNCTION Reduce_Successive
LPARAMETERS lcString

	m.lcString = STRTRAN( m.lcString, [                ], [ ] )
	m.lcString = STRTRAN( m.lcString, [                ], [ ] )
	m.lcString = STRTRAN( m.lcString, [        ], [ ] )
	m.lcString = STRTRAN( m.lcString, [        ], [ ] )
	m.lcString = STRTRAN( m.lcString, [    ], [ ] )
	m.lcString = STRTRAN( m.lcString, [    ], [ ] )
	m.lcString = STRTRAN( m.lcString, [  ], [ ] )
	m.lcString = STRTRAN( m.lcString, [  ], [ ] )
	m.lcString = STRTRAN( m.lcString, [  ], [ ] )  && This line added 2009.07.27

RETURN m.lcString
So, it's possible to reduce series of spaces (or any other characters) down to 1 with a fixed number of STRTRAN() calls:

- up to and including 4,096 spaces with 7 STRTRAN() calls
- up to and including 1,048,576 spaces with 9 STRTRAN() calls
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform