KScript docs
EN

csv fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.headerslistThe names on its first line: ['fruit', 'price'] for a csv that starts fruit,price. Empty when the first line is all numbers, as that line is data.

From KScript.Core

#csv->cells(row?: number, rows?: bool, column?: any, columns?: bool, header?: bool, as?: type) → any

Its cells. On its own, or with rows, every row; with columns, every column. row: picks one row, counted from 0 (-1 is the last); column: picks one column, named by its header or counted from 0. A row and a column together give the one cell where they meet, and rows with column: gives every row's cell in that column. Rows are maps keyed by the header line, or lists of cells when there isn't one; columns are a map of lists keyed by the header line, or a list of lists. Without a header, rows may be different lengths: a column skips the rows too short to reach it. The first line is a header unless every cell on it is a number: say header or not header to choose. Cells written as numbers come out as numbers. Cells may be split by commas, tabs, semicolons, or spaces between numbers. With as: a type, the result is checked and turned into it.
ParameterTakes
row optionalnumber
rows optionala flag: rows or not rows
column optionalany
columns optionala flag: columns or not columns
header optionala flag: header or not header
as optionaltype

Arguments can be named: ->cells(…, rows: …, column: …).

Example
<let name="prices" value="```csv
  fruit,price
  apple,3
  plum,5
  ```" />
<!-- 'apple': the header line names each row's keys -->
<echo>{ $prices->cells(row: 0).fruit }</echo>
<!-- [3, 5]: the cells under the price header, as numbers -->
<echo>{ $prices->cells(column: 'price') }</echo>
<!-- 5: row 1 is plum, and its price is 5 -->
<echo>{ $prices->cells(row: 1, column: 'price') }</echo>
<!-- { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header -->
<echo>{ $prices->cells(columns) }</echo>
<let name="readings" value="```csv
  387
  942
  945
  ```" />
<!-- 2274: every line is a number, so there's no header and every line is a row -->
<echo>{ $readings->cells(column: 0)->sum() }</echo>
<let name="puzzle" value="```csv
  -903 -642 446
  200 818
  ```" />
<!-- [-903, -642, 446]: spaces split the numbers on each line -->
<echo>{ $puzzle->cells(row: 0) }</echo>
<!-- [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column -->
<echo>{ $puzzle->cells(columns) }</echo>
$prices = ```csv
  fruit,price
  apple,3
  plum,5
  ```
// 'apple': the header line names each row's keys
echo $prices->cells(row: 0).fruit
// [3, 5]: the cells under the price header, as numbers
echo $prices->cells(column: 'price')
// 5: row 1 is plum, and its price is 5
echo $prices->cells(row: 1, column: 'price')
// { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header
echo $prices->cells(columns)
$readings = ```csv
  387
  942
  945
  ```
// 2274: every line is a number, so there's no header and every line is a row
echo $readings->cells(column: 0)->sum()
$puzzle = ```csv
  -903 -642 446
  200 818
  ```
// [-903, -642, 446]: spaces split the numbers on each line
echo $puzzle->cells(row: 0)
// [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column
echo $puzzle->cells(columns)

#csv->save(path: string)

Writes the CSV to a file. Adds .csv when the path has no extension.
ParameterTakes
path requiredstring
Example
<let name="prices" value="```csv
  fruit,price
  apple,3
  plum,5
  ```" />
<!-- writes prices.csv: the name has no extension, so .csv is added -->
<do>$prices->save('prices')</do>
$prices = ```csv
  fruit,price
  apple,3
  plum,5
  ```
// writes prices.csv: the name has no extension, so .csv is added
$prices->save('prices')

Methods from any

Methods from string