WRITECD

Updated May 2011

Note: Beginning in A-Shell 5.1.1216 of May 2011, it became possible to specify which delimiter characters would be used for WRITECD and WRITETD operations; see Get/Set CSV Delimited for more information. We have left the original documentation in place because in the absence of a defined delimiter, both WRITECD and WRITETD will work as they previously have. If you define your own delimiter, however, all references below to "comma" should be replaced with "defined delimiter character."

The ability to define a delimiter also means that WRITETD is now deprecated, and should not be used. It remains a valid operator only to preserve operations by older code.

WRITECD (Write Comma Delimited) is a variation of the PRINT statement which is useful for creating industry-standard* comma-delimited files. Syntax and operation is identical to the PRINT statement, except that it performs three additional functions which are useful when more than one variable is being output on a line:

• it automatically inserts a comma between each variable being output

• it automatically quotes any string variable which itself contains any of the "likely" delimiter characters (comma, tab, semicolon, colon), the custom delimiter character (if applicable), or any of the "problem" characters (quote and apostrophe).

• it strips leading and trailing blanks from any string variable (unless accompanied by a formatting mask).

The maximum size of an individual field needing to be enclosed in quotes is 1021 bytes.

Example

WRITECD #1,5.2,"Nov 1, 1998","   abc  "

Outputs:

5.2,"Nov 1,1998",abc

Comments

WRITECD/WRITETD will convert any embedded quotes (") within a field into apostrophes ('), to try to be more compatible with other CSV readers. It will also put double quotes around any field containing quotes or apostrophes, in addition to commas and tabs, as before.

Also note the procedure for breaking up a single line of CSV output into multiple WRITECD statements:

WRITECD #CH, A, B, C,;

WRITECD #CH, D, E, F

 

This will output the six variables on one line. Note that that the comma after the C and before the semicolon is required. If you omit it, the D output will immediately follow C with no delimiter. If you omit the semicolon from the first line, then you'll get two lines of output.

* Note that the term "industry standard" is used with a good deal of sarcasm and distaste in this context, since the notion of any such standard is farcical. Which is why typical spreadsheet programs now employ elaborate 'wizards' to aid in the importing of CSV data, and which despite all kinds of fancy heuristics still have to rely on a complicated user interface for specifying all kinds of options, which if not specified properly, may reduce the results to mush. Readers are hereby advised that if they encounter a CSV file which doesn't read in properly using INPUT CSV, they might need to avail themselves of such a wizard to load the file into a spreadsheet and then re-save it, possibly with a different delimiter. This will hopefully result in a CSV file which has the same ideas about "standard" as does INPUT CSV.

 

History

2011 May, A-Shell 5.1.1216:  Support new custom CSV delimiter; see Get/Set CSV Delimited|topic=MIAMEX 181\: Get/Set CSV Delimiter. Also, expand the list of quoted characters—real and potential delimiters—to: comma, tab, semicolon, colon, quote, apostrophe, plus the custom delimiter character if applicable.