Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.

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 Mike Lewis is a Visual FoxPro developer and trainer. His company, Mike Lewis Consultants Ltd., runs training courses at clients' sites throughout Europe and North America. Please visit his company's web site at www.ml-consult.co.uk for company and contact information.
© Copyright Mike Lewis Consultants Ltd.


Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: