Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert a delimited string to an array
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00259451
Message ID:
00259590
Views:
16
Hi Scott,

The function StrToArray() below parses and places each element
separated by into <@taArray>. The function uses ALINES() which
was added to VFP6.

Example 1:

DECLARE laBidon[1]
lcString = "123,22 44,1212;21412424,12.5,123.4"

lcDelimiter = ",; " && possible delimiters
=StrToArray(@laBidon, lcString, lcDelimiter)

*-- creates the following array:
*-- LABIDON Pub A
*-- ( 1) C "123"
*-- ( 2) C "22"
*-- ( 3) C "44"
*-- ( 4) C "1212"
*-- ( 5) C "21412424"
*-- ( 6) C "12.5"
*-- ( 7) C "123.4"

Example 2:

lcDelimiter = ",;. " && possible delimiters
=StrToArray(@laBidon, lcString, lcDelimiter)

*-- creates 9 fields since the decimal point is also
*-- considered as a delimiter:

*-- LABIDON Pub A
*-- ( 1) C "123"
*-- ( 2) C "22"
*-- ( 3) C "44"
*-- ( 4) C "1212"
*-- ( 5) C "21412424"
*-- ( 6) C "12"
*-- ( 7) C "5"
*-- ( 8) C "123"
*-- ( 9) C "4"

Here's the function:

************************************************
FUNCTION StrToArray
************************************************

*) Description.......:
* Calling Samples...: lnWords = StrToArray(@laWords ,;
* : "123,22 44,1212;21412424,12.5,123.4" ,;
* : ",; " + CHR(9) ;
* : )
* Parameter List....: taArray - The converted array.
* : Must be passed by reference
* : tcString - String to convert
* : tcDelimiter - delimiter(s)
* : ";"
* : ",; " + CHR(9)
* Major change list.:
*--------------------------------------------------------------------------------------------------
LPARAMETER taArray, tcString, tcDelimiter

*-- convert each possible delimiter to CHR(13)
tcString = CHRTRAN(tcString, tcDelimiter, REPL(CHR(13), LEN(tcDelimiter) ) )

*-- convert string to array
lnRetVal = ALINES(taArray, tcString)

*-- return number of array elements
RETURN lnRetVal

*-- EOF Function StrToArray -----------------------------------------------------------------------
Daniel
Previous
Reply
Map
View

Click here to load this message in the networking platform