CSV reading and writing. CsvTable parses delimited text (RFC 4180: quoted fields may hold the delimiter, line breaks and doubled quotes) into CsvRow records and CsvColumn values, reports the first malformed row through GetError(), and writes CSV back out with ToString() or CsvTable->FormatRow(). The delimiter defaults to a comma and can be set per table. Compile with -lib csv.
CsvTable
CSV table. The first record is the header and it is also row 0: Size() counts it (a header and two records is 3), Get(0) is the header record, the data records are Get(1) upward, and GetHeaders() returns the same row 0. So a loop from 0 visits the header first; start at 1 for the data. A table is "parsed" only when it has a header and at least one more record and every record has as many fields as the header: an empty text, a header with no records and a record with too few or too many fields give IsParsed() false, Size() 0 and the reason in GetError() (Get and GetHeaders then return Nil).
Example
csv := CsvTable->New(FileReader->ReadFile("data.csv"));
if(csv->IsParsed()) {
each(i : csv) {
row := csv->Get(i);
row->Get(0)->PrintLine();
};
};Operations
- New
- AppendColumn
- ColumnValues
- Contains
- CountContains
- CountMatches
- Delete
- FormatRow
- Get
- GetDelimiter
- GetError
- GetErrorRow
- GetHeaders
- GetRowId
- IsParsed
- Matches
- RowSize
- Size
- ToJson
- ToString
- UniqueColumnValues
AppendColumn #
Appends a column to the end of the table
method : public : AppendColumn(name:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| name | String | name of column to add |
Return
| Type | Description |
|---|---|
| Bool | true if column was added, false otherwise |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
csv->AppendColumn("score");
Console->PrintLine(csv->RowSize());ColumnValues #
Get values for a given row
method : public : ColumnValues(index:Int) ~ CsvColumnParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
Return
| Type | Description |
|---|---|
| CsvColumn | values for the given row |
Example
csv := CsvTable->New("name,score\r\nAlice,90\r\nBob,85");
col := csv->ColumnValues(1);
Console->PrintLine(col->Sum());ColumnValues #
Get values for a given row
method : public : ColumnValues(name:String) ~ CsvColumnParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| CsvColumn | values for the given row |
Example
csv := CsvTable->New("name,score\r\nAlice,90\r\nBob,85");
col := csv->ColumnValues("score");
Console->PrintLine(col->Average());Contains #
Searches a given column for like values
method : public : Contains(name:String, value:String) ~ CsvTableParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| CsvTable | table of like results |
Example
csv := CsvTable->New("name,role\r\nAlice,Engineer\r\nBob,Manager\r\nCarol,Engineering Lead");
results := csv->Contains("role", "Engin");
Console->PrintLine(results->Size());Contains #
Searches a given column for like values
method : public : Contains(index:Int, value:String) ~ CsvTableParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| CsvTable | table of like results |
CountContains #
Counts a column for like values
method : public : CountContains(name:String, value:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| Int | number of occurrences |
Example
csv := CsvTable->New("name,role\r\nAlice,Engineer\r\nBob,Manager\r\nCarol,Senior Engineer");
count := csv->CountContains("role", "Engineer");
Console->PrintLine(count);CountContains #
Counts a column for like values
method : public : CountContains(index:Int, value:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| Int | number of occurrences |
CountMatches #
Counts a column for matching values
method : public : CountMatches(name:String, value:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| Int | number of occurrences |
Example
csv := CsvTable->New("name,dept\r\nAlice,Eng\r\nBob,HR\r\nCarol,Eng");
count := csv->CountMatches("dept", "Eng");
Console->PrintLine(count);CountMatches #
Counts a column for matching values
method : public : CountMatches(index:Int, value:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| Int | number of occurrences |
Delete #
Removes a row from the table
method : public : Delete(id:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| id | Int | row to delete |
Return
| Type | Description |
|---|---|
| Bool | true if deleted false otherwise |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
csv->Delete(1);
Console->PrintLine(csv->Size());FormatRow # function
Formats values as one CSV record with comma delimiters. A field is quoted only when it must be: when it holds the delimiter, a quote, a line break, or white space at either end. A quote inside a field is doubled. Nil is written as an empty field. CsvTable parses the result back to the same values.
function : FormatRow(values:String[]) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| values | String | field values |
Return
| Type | Description |
|---|---|
| String | CSV record, without a line ending |
Example
line := CsvTable->FormatRow(["plain", "has,comma", "say \"hi\""]);
line->PrintLine();FormatRow # function
Formats values as one CSV record with the given delimiter, quoting a field only when it must be (see the one-argument form)
function : FormatRow(values:String[], delimiter:Char) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| values | String | field values |
| delimiter | Char | field delimiter |
Return
| Type | Description |
|---|---|
| String | CSV record, without a line ending |
Example
line := CsvTable->FormatRow(["a", "b c", "d"], '\t');
line->PrintLine();Get #
Gets an indexed row
method : public : Get(index:Int) ~ CsvRowParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| CsvRow | row |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
row := csv->Get(1);
row->Get("name")->PrintLine();GetDelimiter #
Gets the field delimiter
method : public : GetDelimiter() ~ CharReturn
| Type | Description |
|---|---|
| Char | field delimiter |
Example
csv := CsvTable->New("a;b\n1;2", ';');
csv->GetDelimiter()->PrintLine();GetError #
Gets why the data could not be parsed: the first malformed row, with its row number (0 is the header) and the line it starts on, or a missing closing quote, or too few rows. A table that parsed returns Nil.
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | error message, or Nil if the table parsed |
Example
csv := CsvTable->New("a,b,c\n1,2,3\n4,5");
if(<>csv->IsParsed()) {
csv->GetError()->PrintLine();
};GetErrorRow #
Gets the row the parse failed on (0 is the header), or -1 when the table parsed or the failure is not tied to one row
method : public : GetErrorRow() ~ IntReturn
| Type | Description |
|---|---|
| Int | failing row number, or -1 |
Example
csv := CsvTable->New("a,b,c\n1,2,3\n4,5");
Console->PrintLine(csv->GetErrorRow());GetHeaders #
Gets header names
method : public : GetHeaders() ~ CsvRowReturn
| Type | Description |
|---|---|
| CsvRow | header names |
Example
csv := CsvTable->New("name,age,city\r\nAlice,30,NY");
headers := csv->GetHeaders();
headers->Get(0)->PrintLine();
headers->Get(1)->PrintLine();GetRowId #
Gets row name
method : public : GetRowId(name:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| name | String | name |
Return
| Type | Description |
|---|---|
| Int | row index |
Example
csv := CsvTable->New("name,age,city\r\nAlice,30,NY");
Console->PrintLine(csv->GetRowId("age"));IsParsed #
Returns rather the file has been successfully parsed
method : public : IsParsed() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if successfully parsed, false otherwise |
Example
csv := CsvTable->New("name,age\r\nAlice,30");
if(csv->IsParsed()) {
"parsed ok"->PrintLine();
};Matches #
Searches a given column for matching values
method : public : Matches(name:String, value:String) ~ CsvTableParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| CsvTable | table of matching results |
Example
csv := CsvTable->New("name,dept\r\nAlice,Eng\r\nBob,HR\r\nCarol,Eng");
results := csv->Matches("dept", "Eng");
Console->PrintLine(results->Size());Matches #
Searches a given column for matching values
method : public : Matches(index:Int, value:String) ~ CsvTableParameters
| Name | Type | Description |
|---|---|---|
| index | Int | column index |
| value | String | value to search for |
Return
| Type | Description |
|---|---|
| CsvTable | table of matching results |
New # constructor
Constructor. Fields are separated by commas. A record ends at a CRLF, an LF or a lone CR outside quotes, so a file written on any platform parses without the caller knowing which; ToString() writes back the first ending the data used. Use the two-argument constructor to force one ending.
New(data:String)Parameters
| Name | Type | Description |
|---|---|---|
| data | String | CSV data |
Example
csv := CsvTable->New("name,age\nAlice,30\nBob,25");
if(csv->IsParsed()) {
Console->PrintLine(csv->Size());
};New # constructor
Constructor that forces the line ending: only this ending ends a record, and any other line break is kept as part of a field.
New(data:String, ending:String)Parameters
| Name | Type | Description |
|---|---|---|
| data | String | CSV data |
| ending | String | line ending |
Example
csv := CsvTable->New("name,age\nAlice,30\nBob,25", "\n");
if(csv->IsParsed()) {
Console->PrintLine(csv->Size());
};New # constructor
Constructor with a custom delimiter, such as a tab or a semicolon. The line ending is taken from the data, as for the one-argument constructor.
New(data:String, delimiter:Char)Parameters
| Name | Type | Description |
|---|---|---|
| data | String | CSV data |
| delimiter | Char | field delimiter; not a quote, CR or LF |
Example
csv := CsvTable->New("name;age\nAlice;30\nBob;25", ';');
if(csv->IsParsed()) {
csv->Get(1)->Get("age")->PrintLine();
};New # constructor
Constructor with a forced line ending and a custom delimiter
New(data:String, ending:String, delimiter:Char)Parameters
| Name | Type | Description |
|---|---|---|
| data | String | CSV data |
| ending | String | line ending |
| delimiter | Char | field delimiter; not a quote, CR or LF |
Example
csv := CsvTable->New("name\tage\r\nAlice\t30", "\r\n", '\t');
if(csv->IsParsed()) {
csv->Get(1)->Get("name")->PrintLine();
};RowSize #
Gets the size of rows
method : public : RowSize() ~ IntReturn
| Type | Description |
|---|---|
| Int | row size |
Example
csv := CsvTable->New("a,b,c\r\n1,2,3");
Console->PrintLine(csv->RowSize());Size #
Gets the number of rows
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | number of rows |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25\r\nCarol,28");
Console->PrintLine(csv->Size());ToJson #
Formats the table into a JSON object string
method : public : ToJson() ~ StringReturn
| Type | Description |
|---|---|
| String | JSON object string |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
csv->ToJson()->PrintLine();ToString #
Formats the table into a string
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation of the table |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
csv->ToString()->PrintLine();UniqueColumnValues #
Get unique values for a given row
method : public : UniqueColumnValues(name:String) ~ CsvColumnParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| CsvColumn | unique values for the given row |
Example
csv := CsvTable->New("name,dept\r\nAlice,Eng\r\nBob,HR\r\nCarol,Eng");
uniq := csv->UniqueColumnValues("dept");
Console->PrintLine(uniq->Size());