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.
CsvRow
CSV row
Example
csv := CsvTable->New(FileReader->ReadFile("data.csv"));
row := csv->Get(0);
row->Get(0)->PrintLine(); # by index
row->Get("name")->PrintLine(); # by column name
row->Size()->PrintLine(); # column countOperations
Append #
Appends value to the end of the row
method : public : Append(value:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| value | String | to append |
Example
csv := CsvTable->New("name,age\r\nAlice,30");
row := csv->Get(1);
row->Append("engineer");
Console->PrintLine(row->Size());Average #
Calculates the row average
method : public : Average() ~ FloatReturn
| Type | Description |
|---|---|
| Float | row average |
Example
csv := CsvTable->New("a,b,c\r\n10,20,30");
row := csv->Get(1);
Console->PrintLine(row->Average());Average #
Calculates the row average
method : public : Average(end:Int) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| end | Int | ending column |
Return
| Type | Description |
|---|---|
| Float | row average |
Average #
Calculates the row average
method : public : Average(start:Int, end:Int) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| start | Int | starting column |
| end | Int | ending column |
Return
| Type | Description |
|---|---|
| Float | row average |
Get #
Gets the indexed row value
method : public : Get(index:Int) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
Return
| Type | Description |
|---|---|
| String | row value |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
row := csv->Get(1);
row->Get(0)->PrintLine();
row->Get(1)->PrintLine();Get #
Gets the indexed row value
method : public : Get(name:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| name | String | name |
Return
| Type | Description |
|---|---|
| String | row value |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
row := csv->Get(1);
row->Get("name")->PrintLine();
row->Get("age")->PrintLine();Id #
Gets the row ID
method : public : Id() ~ IntReturn
| Type | Description |
|---|---|
| Int | id |
Example
csv := CsvTable->New("name,age\r\nAlice,30\r\nBob,25");
row := csv->Get(1);
Console->PrintLine(row->Id());Median #
Calculates the row median
method : public : Median() ~ FloatReturn
| Type | Description |
|---|---|
| Float | row median |
Example
csv := CsvTable->New("a,b,c,d\r\n1,3,5,7");
row := csv->Get(1);
Console->PrintLine(row->Median());New # constructor
Constructor
New(table:CsvTable, id:Int, read_only:Bool, columns:CompareVector<String>)Parameters
| Name | Type | Description |
|---|---|---|
| table | CsvTable | table that has row |
| id | Int | row id |
| read_only | Bool | true if readonly, false otherwise |
| columns | CompareVector<String> | column values |
Set #
Sets the indexed row value
method : public : Set(index:Int, value:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| index | Int | index |
| value | String | value to set |
Return
| Type | Description |
|---|---|
| Bool | true of value was set, false otherwise |
Example
csv := CsvTable->New("name,score\r\nAlice,0\r\nBob,0");
row := csv->Get(1);
row->Set(1, "95");
row->Get(1)->PrintLine();Set #
Sets the named row value
method : public : Set(name:String, value:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| name | String | name |
| value | String | value |
Return
| Type | Description |
|---|---|
| Bool | true of value was set, false otherwise |
Example
csv := CsvTable->New("name,score\r\nAlice,0\r\nBob,0");
row := csv->Get(1);
row->Set("score", "88");
row->Get("score")->PrintLine();Size #
Gets the row size
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | row size |
Example
csv := CsvTable->New("a,b,c\r\n1,2,3");
row := csv->Get(1);
Console->PrintLine(row->Size());Sum #
Calculates the row sum
method : public : Sum() ~ FloatReturn
| Type | Description |
|---|---|
| Float | row sum |
Example
csv := CsvTable->New("a,b,c\r\n10,20,30");
row := csv->Get(1);
Console->PrintLine(row->Sum());Sum #
Calculates the row sum
method : public : Sum(end:Int) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| end | Int | ending column |
Return
| Type | Description |
|---|---|
| Float | row sum |
Sum #
Calculates the row sum
method : public : Sum(start:Int, end:Int) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| start | Int | starting column |
| end | Int | ending column |
Return
| Type | Description |
|---|---|
| Float | row sum |
ToJson #
Formats the row into a JSON object string
method : public : ToJson() ~ StringReturn
| Type | Description |
|---|---|
| String | JSON object string |
Example
csv := CsvTable->New("name,age\r\nAlice,30");
row := csv->Get(1);
row->ToJson()->PrintLine();ToString #
Formats the row as one CSV record: every field is quoted, a quote inside a field is doubled, and fields are separated by the table's delimiter. CsvTable parses the result back to the same values.
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation of the row |
Example
csv := CsvTable->New("name,age\r\nAlice,30");
row := csv->Get(1);
row->ToString()->PrintLine();