Populating CSV Lists

The data in a CSV list is structured using special characters known as Column Separators and Row Separators, allowing the application to interpret it in a tabular fashion. As such, each Row must contain the exact same number of values. Otherwise, it is not possible to accurately identify which column each value belongs to.

The following is a valid example of CSV structure, with a comma used as the column separator and a pipe used as the row separator:

A,B,C|D,E,F|G,H,I|

The row separator signifies the end of a distinct data row, while column separators distinguish individual values in a row. The example above represents a CSV with three rows, each consisting of three values. Therefore, the table represented by this data consists of three rows and three columns.

On the other hand, the following shows an example of invalid CSV:

A,B,C|D,E|F,G,H,I|

Although this example contains the same total number of values, and is also divided into three rows, the first, second and third rows all contain a different number of values - three, two and four, respectively. As a result, it is not possible to determine the number of data columns, which is why the CSV is invalid.

The simplest way to add CSV data to a variable is to use regular variable assignment, whether that be via assignment expressions or target model variables.

For example, this expression would assign a new set of data to a variable called MyCSV:

MyCSV:= "A,B,C|D,E,F|";

After using the expression above, you could use a similar expression to add a new row, like so:

MyCSV:= "A,B,C|D,E,F|G,H,I|";

However, the problem with expressions that use literal values is that they have very limited use; if the existing contents of the CSV are not "A,B,C|D,E,F|", the actual values will be overwritten. Also, what if you don't want to add "G,H,I" to the CSV?

It would be better to use an expression that concatenates the existing value of the CSV with another value taken from a question, variable or other location. If you want MyCSV to receive a new value from the RowToAdd variable, you would use the following expression:

MyCSV:= MyCSV & RowToAdd;

You can use this expression as many times as you like to add multiple rows of data to the existing CSV.

This is a simple solution, but it may not give you sufficient control over the content of your CSV. After initialising the CSV, you can modify its contents using functions.

To learn how to manage CSV lists using functions, refer to the following topics:

Description Further Reading
Add a single row to a CSV list. CSVListAddRow
Search the CSV list for a specific set of data, and remove the first instance found. CSVListRemoveRow
Search the CSV list for a specific set of data, and return a value of True if the data exists or False if not. CSVListItemExists
Perform a specific action against each row in a CSV list in turn. ForEachCSVListItem