CDS031

Convert CSV String to Array

Last Revised: 05/20/14

This program can be used to convert a tab-separated, comma-separated, or pipe-separated string into a string array.  These CSV strings are often created using a spreadsheet program or other data export tool. When used with the CHAN option, CDS031 can be used to read the data from the csv file, which provides support for field and line separators imbedded within quoted text strings. This enables quoted text strings to contain commas, tabs, carriage-return, and line-feed characters.

Calling Format:

CALL "CDS031", CSV$, CVS, FLD$[ALL], FLDS {, FLDS$, CHAN }

CDS031
CSV$
CHAN not passed Input string containing the separated fields. If passed as null then CDS031 will assume a single null string
CHAN passed Output string read from file opened to channel CHAN. Will be null when end of file is reached.
CVS used by CDS031 with the CVS() function when assigning fields, and should be set to the sum of the functions desired below. See CVS() in the BBx documentation for additional information
0no action
1strip leading blanks
2strip trailing blanks
4convert to upper case
8convert to lower case
16replace non-displayable characters with blanks
32replace multiple spaces with a single space
64replace "," and "." characters with values specified in OPTS
FLD$[ALL] will be returned by CDS031 with one element of the array for each field in the record.  The leading and trailing quotation marks will be removed from field that has a quotation mark at the beginning and at the end of the field.  Since some export programs place quotes around strings, they often store a quote contained within the field as two quotes.  CDS031 converts a pair of adjacent quote marks into a single quote mark.  The first field is FLD$[1] and the last field is FLD$[FLDS].
FLDS set by CDS031 to the number of elements in the FLD$[ALL] array.
FLDS$ optional variable set by CDS031 which is similar to FLD$[ALL] except that it is in template format.  The first field is FLDS.FLD$[1] and the last field is FLDS.FLD$[FLDS].
CHAN

Optional channel number which can be passed to CDS031 to have it perform the read operations to a string file used as input. Some CSV files include fields that have line feeds that within a quoted field indicating multiple lines of text. Since a line feed also separates records, filtering out these line feeds imbedded within a quotes field can be tricky. By passing CHAN instead of CSV$, CDS031 will read the file opened by the calling program to channel CHAN, and merge the multiple lines within a quoted field into a single field. You can use this option even when you do not expect to have line feeds imbedded within a single field.

When the end of the file is reached, CDS031 will return CSV$ as null.

CDS031 determines the separation character automatically, which can be a tab ($09$), a comma, or the pipe (|) symbol.

When using comma separated fields, the field can contain commas if the field starts and ends with a quotation mark.

It is generally preferred to use a tab as the field separator, as unlike a comma or the pipe symbol, a field cannot contain the tab character.

Example using comma separator
>CALL "CDS031","123,456,789",0,FLD$[ALL]
>WRITE FLD$[ALL]
123
456
789
Example using pipe separator
>CALL "CDS031","abc|d|efghi|jkl",4,FLD$[ALL],FLDS
>?FLDS
4
>WRITE FLD$[ALL]
ABC
D
EFGHI
JKL
Example of a field that contains a pair of adjacent quotes
>CSV$="12"""" NAILS,1.23,SMALL"
>?CSV$
12"" NAILS,1.23,SMALL
>CALL "CDS031",CSV$,0,FLD$[ALL],FLDS
>?FLDS
3
>WRITE FLD$[ALL]
12" NAILS
1.23
SMALL
Example using comma separator with a field that contains commas
>CSV$="""Cable, Parallel Printer, 6 ft"",7.50,33"
>?CSV$
"Cable, Parallel Printer, 6 ft",7.50,33
>call "CDS031",CSV$,0,FLD$[ALL],FLDS
>?FLDS
3
>WRITE FLD$[ALL]
Cable, Parallel Printer, 6 ft
7.50
33