Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert a delimited string to an array
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00259451
Message ID:
00259684
Vues:
22
>The title says it all. Suppose you have any of the following strings:
>
>123 12 456
>123456
>123, 32,99
> 999, 88 777777,
>,123,
>
>You and I can see through the delimiters and user input anomalies, but I'd like to have a function to pick out the data and ignore the delimiters, and put the data into an array.
>

Also known as CHOP() in Perl... here is my VFP version of chop... Some taken from Hackers guide:
* Procedure Chop
* This will take a delimeted string and parse into array elements

Lparameters cInput, aValues, cDelimeter
* cInput is the input string
* store the results in array aValues with one item per element
* cDelimeter is the delimeter to search for... default to comma
*   if not passed

local cAssert

cAssert = Set('Assert')
Set Assert On
Assert VarType( cInput ) == "C" Message "cInput must be passed as a Character!"
Assert Type( 'aValues[1]' ) <> "U" Message "aValues must be an array passed by Reference!"

If ! cDelimeter
	cDelimeter = ','
Endif

nItems=Occurs(cDelimeter,cInput)
Dimension aValues[nItems+1]

For nCnt=1 To nItems
	* find the first Delimeter
	nCommaPos=At(cDelimeter,cInput)

	* grab the string before it
	aValues[nCnt]=Left(cInput,nCommaPos-1)

	* shorten the string
	If nCommaPos<>LEN(cInput)
		cInput=SUBSTR(cInput,nCommaPos+1)
	Else
		cInput=""
	Endif
Endfor

* now take last item
* check just in case there was a trailing comma
If Empty(cInput)
	Dimension aValues[nItems]
Else
	aValues[nItems+1]=cInput
Endif

Set Assert &cAssert
*******
BOb
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform