Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Quickly alltrim non-alpha chars from string
Message
From
06/08/2005 16:28:16
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9
Miscellaneous
Thread ID:
01037589
Message ID:
01039107
Views:
23
>I didn't see this thread until now... I worked on this problem on profox. For shorter strings I don't think the nested chrtran() approach can be beat, though I haven't timed the regular expressions idea. For longer strings Visual Foxpro can be handed it's hat by using an FLL. Here's a link to the FLL I wrote and the speed test code for it....
>
>http://www.sweetpotatosoftware.com/files/nonalphatrim.zip
>
>
******************************************************
>*!* Run Sample speed trial between FLL and native VFP code
>
>SET LIBRARY TO (GETFILE()) && locate the nonalphatrim.fll
>
>x = SECONDS()
>FOR i = 1 TO 10000 && FLL run takes 0.356 seconds on my machine
>	TrimNonAlpha(REPLICATE(CHR(12) + CHR(13) + "12A" + CHR(13) + CHR(10) + "34ABC1234", 100)) ENDFOR
>?SECONDS() - x
>SET LIBRARY TO
>
>x = SECONDS()
>FOR i = 1 TO 10000 && Visual FoxPro run takes 25.818 seconds on my machine (72 times slower)
>	TrimNonAlphas(REPLICATE(CHR(12) + CHR(13) + "12A" + CHR(13) + CHR(10) + "34ABC1234", 100)) ENDFOR
>?SECONDS() - x
>
>FUNCTION TrimNonAlphas(tcPhrase)
>	#DEFINE ALPHAS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
>	LOCAL lcAlphaPhrase, lcPhrasePart
>	lcAlphaPhrase = CHRTRAN(tcPhrase, CHRTRAN(tcPhrase, ALPHAS, ""), "")
>	lcPhrasePart = SUBSTR(tcPhrase, AT(LEFT(lcAlphaPhrase, 1), tcPhrase)) RETURN LEFT(lcPhrasePart, RAT(RIGHT(lcAlphaPhrase, 1), lcPhrasePart))
>*************************************************
>
>

Hi Craig,

don't to use CHRTRAN(...,cF,cR) with LEN(cF)>LEN(cR),
because it is implemented with a right to left MoveMem for every cF removed.

The following code suffers less:
#define _AlphaCharacters_		ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
#define _AlphaCharactersLen_	120

CLEAR

******************************************************
*!* Run Sample speed trial between FLL and native VFP code

SET LIBRARY TO (GETFILE()) && locate the nonalphatrim.fll

cWord = REPLICATE("__12A__34ABC1234", 100)

x = SECONDS()

FOR i = 1 TO 10000 && FLL run takes 0.356 seconds on my machine
	TrimNonAlpha(m.cWord)
ENDFOR
?SECONDS() - x
SET LIBRARY TO

x = SECONDS()
FOR i = 1 TO 10000 && Visual FoxPro run takes 25.818 seconds on my machine (72 times slower)
	TrimNonAlphas(@m.cWord)
ENDFOR
?SECONDS() - x

x = SECONDS()
FOR i = 1 TO 10000 && Visual FoxPro run takes ???? seconds on my machine (10 times slower)
	AllTrimNotAlpha(@m.cWord)
ENDFOR
?SECONDS() - x

FUNCTION TrimNonAlphas(tcPhrase)
	#DEFINE ALPHAS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	LOCAL lcAlphaPhrase, lcPhrasePart
	lcAlphaPhrase = CHRTRAN(tcPhrase, CHRTRAN(tcPhrase, ALPHAS, ""), "")
	lcPhrasePart = SUBSTR(tcPhrase, AT(LEFT(lcAlphaPhrase, 1), tcPhrase))
	RETURN LEFT(lcPhrasePart, RAT(RIGHT(lcAlphaPhrase, 1), lcPhrasePart))
*************************************************

function AllTrimNotAlpha( pcStr )
#define _NotAlphaCharacters_	0h000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F405B5C5D5E5F607B7C7D7E7F8081828485868788898B8D8E8F909192939495969798999B9D9EA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFD7F7
#define _NotAlphaCharactersLen_	136
	PRIVATE cDummy
	STORE RTRIM(CHRTRAN(m.pcStr ,_NotAlphaCharacters_,	SPACE(_NotAlphaCharactersLen_))) TO cDummy
	RETURN RIGHT(LEFT(m.pcStr ,LEN(m.cDummy)),LEN(LTRIM(m.cDummy)))
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform