CSV

The CSV: module parses, builds, and serializes RFC 4180 CSV data. Documents are named and held in memory until freed. Rows and columns are 0-based.


Call Effect
CSV:Parse(str, name) Parse CSV string into named document (comma delimiter)
CSV:Parse(str, delim, name) Parse with custom single-character delimiter
CSV:Get(row, col, name, outVar) Read cell → outVar ("" if out of range)
CSV:Set(row, col, val, name) Write cell; extends document as needed
CSV:Rows(name, outVar) / CSV:RowCount Row count → outVar
CSV:Cols(row, name, outVar) / CSV:ColumnCount Column count for given row → outVar
CSV:Stringify(name, outVar) / CSV:ToString Serialize to CSV string → outVar
CSV:Stringify(delim, name, outVar) Serialize with custom delimiter
CSV:Free(name) / CSV:Release Release document
Var:String src = "name,age\nAlice,30\nBob,25";
CSV:Parse(src, data);

Var:Number rows;
CSV:Rows(data, rows);
Konsol:Print(rows);     // 3

Var:String cell;
CSV:Get(1, 0, data, cell);
Konsol:Print(cell);     // Alice

// Build from scratch
CSV:Set(0, 0, "product", out);
CSV:Set(0, 1, "price", out);
CSV:Set(1, 0, "apple", out);
CSV:Set(1, 1, "1.20", out);

Var:String csv;
CSV:Stringify(out, csv);
Konsol:Print(csv);

CSV:Free(data);
CSV:Free(out);

Examples

// 38_csv.ks — CSV module

Var:String s;
Var:Number n;

Konsol:Print("=== parse flat ===");
CSV:Parse("name,age,city\nAlice,30,Manila\nBob,25,Cebu\nCarla,28,Davao", data);
CSV:Rows(data, n);   Konsol:Print(n);           // 4 (header + 3 rows)
CSV:Cols(0, data, n); Konsol:Print(n);          // 3

CSV:Get(0, 0, data, s); Konsol:Print(s);        // name
CSV:Get(0, 1, data, s); Konsol:Print(s);        // age
CSV:Get(1, 0, data, s); Konsol:Print(s);        // Alice
CSV:Get(1, 1, data, s); Konsol:Print(s);        // 30
CSV:Get(2, 2, data, s); Konsol:Print(s);        // Cebu
CSV:Get(3, 0, data, s); Konsol:Print(s);        // Carla

Konsol:Print("=== quoted fields ===");
CSV:Parse("id,note\n1,\"hello, world\"\n2,\"she said \"\"hi\"\"\"", quoted);
CSV:Get(1, 1, quoted, s); Konsol:Print(s);      // hello, world
CSV:Get(2, 1, quoted, s); Konsol:Print(s);      // she said "hi"

Konsol:Print("=== custom delimiter ===");
CSV:Parse("a\tb\tc\n1\t2\t3", "\t", tsv);
CSV:Get(0, 1, tsv, s); Konsol:Print(s);         // b
CSV:Get(1, 2, tsv, s); Konsol:Print(s);         // 3

Konsol:Print("=== set cell ===");
CSV:Set(1, 0, "Alicia", data);
CSV:Get(1, 0, data, s); Konsol:Print(s);        // Alicia

Konsol:Print("=== build from scratch ===");
CSV:Set(0, 0, "x", out);
CSV:Set(0, 1, "y", out);
CSV:Set(1, 0, "10", out);
CSV:Set(1, 1, "20", out);
CSV:Stringify(out, s);
Konsol:Print(s);    // x,y\n10,20\n

Konsol:Print("=== stringify with delimiter ===");
CSV:Stringify("\t", tsv, s);
Konsol:Print(s);    // a\tb\tc\n1\t2\t3\n

Konsol:Print("=== loop over rows ===");
CSV:Rows(data, n);
for (Var:Number r = 1; r < n; r++) {
    Var:String name;
    Var:String city;
    CSV:Get(r, 0, data, name);
    CSV:Get(r, 2, data, city);
    Konsol:Print(name + " - " + city);
}
// Alicia - Manila
// Bob    - Cebu
// Carla  - Davao

Konsol:Print("=== free ===");
CSV:Free(data);
CSV:Free(quoted);
CSV:Free(tsv);
CSV:Free(out);
Konsol:Print("done");