Difference between revisions of "MakeCSV"
Revision as of 21:08, 13 May 2016
new to Analytica 5.0
- 1 MakeCSV( a, columnIndex, rowIndex, columnHeaders, rowHeaders, separator, quoteFlags, dateTemplate, decimal, quote )
- 2 Examples
- 3 See Also
MakeCSV( a, columnIndex, rowIndex, columnHeaders, rowHeaders, separator, quoteFlags, dateTemplate, decimal, quote )
Makes CSV-formatted text from a two-dimensional array «a» which is indexed by «columnIndex» and «rowIndex». CSV stands for "Comma separated text", but may use a separator other than a comma in practice. Each row is separated in the result by a new-line (CR), and within a row each item is separated by a comma by default, or by «separator».
Null values are blank, with no space between the leading and separators.
Row and Column Headers
A first line can be optionally included containing the column names. If you want a column header line, specify the names of the columns to «columnHeaders». The value you pass must be indexed by «columnIndex». When your column index contains the labels you want to use, simple pass that same index to «columnHeaders». If you omit «columnHeaders», the first line contains the first row of data.
You can optionally prepend a first column containing row headers. To do so, specify the row header labels or values in the «rowHeaders» parameter. The value you pass must be indexed by «rowIndex». When your row index already contains the row headers, pass the same row index for «rowHeaders».
Changing the field separator
To use a separator other than comma, specify it in the «separator» parameter. «Separator» can contain multiple characters.
A commonly used separator is the tab-character, which you can specify as
Chr(9), or you can type a tab character in a definition with the three keystrokes quote-tab-quote.
How values are quoted
By default, numbers and dates are not quoted, text is always quoted, and text value that would parse as numbers or dates are escape quoted with an equal sign followed by quotes, like
The «quoteFlags» parameter is a bit-field, where you can add any of the following flags together.
- 1 = No quotes around text (except when required)
- 2 = No escape when text parses as number
- 4 = No escape when text parses as date
- 8 = Quotes around numbers
- 16 = Quotes around dates
Quotes are required whenever the text contains a newline or separator character. Inside quoted text, if a quote appears within the text, it is doubled, so for example text with one quote in the middle appears like
"one "" inside".
The «dateTemplate» controls how date-time numbers are written. To control the format of date-time numbers, you can specify a template using the same format used for a custom date format in the Number Format Dialog. For example, a «dateTemplate» of "dd/MM/yyyy" would write
Changing the decimal point character
The «decimal» parameter specifies the character used to separate the whole number from the fractional part of a number. When not specified, this defaults to the standard convention in the United States of using a dot, e.g.,
3.14. To use a comma, use
MakeCSV(..., separator:';', decimal:','), which writes
3,14. MakeCSV does not write thousand separators within numbers. When you change the «decimal» to comma, it is recommended that you use a «separator» other than comma, such as a semi-colon.
By default, the double quote character (
") is used. You can specify a different character to use in the «quote» parameter. For example, to use single quotes use
MakeCSV(..., quote:'). The value here is typed as four single quotes, which is one way to type a text literal in Analytica consisting of a single character, which happens to be the single quote character. (Another way to type it is doubleQuote - singleQuote - doubleQuote).
Create a text value in standard CSV form from an array
Salary indexed by
MakeCSV( Salary, Time, Person )
This puts each person on a separate line, and the columns across correspond to Time. There are so row or column headers, so the first line of data is the first person. Here is a sampling (with 5 Time points and 3 Persons):
45600,46500,48800,51000,52000 ,,71000,73000,77500 120000, 120000, 125000, 125000, 125000
To write the same data to a csv file, use
WriteTextFile( "salary data.csv", MakeCSV( Salary, Time, Person ) )
In include the person as the first field on each line, and the Time for each column on the first line, use
MakeCSV( Salary, Time, Person, columnHeaders:Time, rowHeaders:Person )
"Person",2011,2012,2013,2014,2015 "00001",45600,46500,48800,51000,52000 "00002",,,71000,73000,77500 "00003",120000, 120000, 125000, 125000, 125000
In the CSV here, the «Person» index contains textual ID numbers with leading zeros.
Assuming FirstName and LastName are indexed by Person, full names can be used for the row header by using
MakeCSV( Salary, Time, Person, columnHeaders:Time, rowHeaders:FirstName & ' ' & LastName)
"Person",2011,2012,2013,2014,2015 "Alice Zen",45600,46500,48800,51000,52000 "Bob Yarbough",,,71000,73000,77500 "Carol Xian",120000, 120000, 125000, 125000, 125000