Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Single occurrence in a string
Message
De
28/03/2015 09:59:04
 
 
À
27/03/2015 16:15:33
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Network:
Windows 2008 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01617326
Message ID:
01617345
Vues:
78
>Thanks to all (Dragan, Marcia and Tore). I wonder which solution is the fastest?
>
>>How do I remove duplicate characters in a string:

Depends a lot on length of the string to dedupe and how many different letters are in it.
If you have a novel-length string, Dragans code will probably be slowest.

My base idea for such a scenario was a variation of Marcias approach:
FUNCTION DeDupe(tcString)
LOCAL lcNewString
lcNewString = ""
DO WHILE LEN(m.tcString)>0  && perhaps faster: ! m.tcString==""
	lcNewString = m.lcNewString + Left(m.tcString, 1)
	tcString = CHRTRAN(m.tcString,Left(m.tcString, 1),"")
ENDDO 
RETURN m.lcNewString
I expect my solution to be much faster than Tores if there are less than 5 different letters in a string of novel length (something like finding the acids in genome analysis), and as the number of different asc() values found in the string increases the time difference to decrease. But:

NB!
Tore uses undocumented vfp features in his SQL solution, which might not work in the next version ;-)))
In SQL portable across many backends:
FUNCTION CommonSql
Lparameters tcString
Local lcReturn As String, ;
   lnX As Number
Create Cursor curDummy (myChar Char(1), nPos I)
For m.lnX = 1 To Len(m.tcString)
   Insert Into curDummy Values (Substr(m.tcString, m.lnX, 1), m.lnX)
Endfor
Select myChar, MIN(nPos) ;
	From curDummy ;
	Into Cursor curDummy ;
	GROUP BY 1 ;
	ORDER BY 2
	
m.lcReturn = []
Scan
   m.lcReturn = m.lcReturn + myChar
endscan
use in CurDummy
Return m.lcReturn
In favor of the SQL/Cursor approach is the fact that the timings should be more robust/invariant to changes of count and distribution of count across the axis of elements found. In Marcias/my approach the time will be shorter if the chars occurring the highest number of times are found first compared to found last.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform