Company logo

FOXSTUFF

Advice, tips, techniques and downloads for Visual Foxpro developers.

Home | Contact

Versión en español de este artículo

Parsing comma-delimited strings into a Visual Foxpro array

An easy way of unpacking comma-delimited data.

In Visual FoxPro (and other programming languages), comma-delimited strings are a handy way of storing small collections of data. It's common to use these strings to pass parameters to procedures or functions, or to store values in custom properties of a class. You might also import data from other applications in the form of a comma-delimited text file.

Here is an easy way of unpacking a comma-delimited string in VFP 6.0. Using this single line of code, you can quickly extract each of the comma-separated items in the string and place it in a separate element of an array:

nRows = ALINES(laData, STRTRAN(cTest,",",CHR(13)))

In this case, the comma-delimited string is called cTest and the array is called laData. The array will be created if it doesn't already exists. After the code is executed, the variable nRows will contain the number of elements in the array.

The code performs two simple operations. First, the STRTRAN() function converts each of the commas to an end-of-line character (ASCII 13). The ALINES() function then places each of the "lines" so formed into an element of the array. ALINES(), which was introduced with VFP 6.0, is designed to extract paragraphs from long text strings, but it works just as well in the context shown here.

In VFP 7.0 and later, the code is even simpler. VFP 7.0 introduced an additional parameter to ALINES() which lets you specify a custom end-of-line character. So instead of having to use STRTRAN() to convert the commas to ASCII 13, you simply stipulate that the comma marks the end of the line. The code looks like this:

nRows = ALINES(laData, cTest, .F., ",")

The third parameter to ALINES()  is a flag that says whether the "line" should be trimmed when it is copied to the array.

If you want to convert an entire text file into an array, simply read it into a character string before applying the above code. In VFP 6.0 and above, the easiest way to read a file into a string is with the FILETOSTR() function. So, given a comma-delimited file called CLIENTS.TXT, here is how you would parse each field into an array called aClients:

cClients = FILETOSTR("clients.txt") 
nRows = ALINES(aClients, STRTRAN(cClients,",",CHR(13)))

Note that this technique won't work if the variables themselves contain commas. When storing names and addresses as comma-separated variables, for example, it is customary to put quotes around any address lines that contain commas, to tell the parser to treat each of these as a single element. Unfortunately, the code shown in this article can't handle that situation.

Mike Lewis Consultants Ltd. July 2000. Revised November 2001.

More Visual FoxPro articles | Crystal Reports articles | Recommended books | Visual FoxPro consultancy | Contact us

FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).

The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.

© Copyright Mike Lewis Consultants Ltd.