CSV
A csv value is text that holds valid CSV: rows of cells split by commas, one row per line. Write it as a ```csv block, or give a variable the csv type and quoted text. Either way, a line that doesn't read as CSV (a quote that never closes, say) is an error where it is written, in the editor and when the script runs.
<const name="prices">```csv
fruit,price
apple,3
plum,5
```</const>
<let name="totals" type="csv" value="'month,total'" />const $prices = ```csv
fruit,price
apple,3
plum,5
```
$totals: csv = 'month,total'The first line usually names the columns. .headers reads them:
<echo value="$prices.headers" /> <!-- ['fruit', 'price']: the first line names the columns -->echo $prices.headers // ['fruit', 'price']: the first line names the columnsReading cells
->cells() reads the cells of the csv. On its own, or with the rows flag, it gives every row; with columns, every column. row: picks one row and column: one column. Rows count from 0 (-1 is the last), and a column is named by its header or counted from 0:
<echo value="$prices->cells()" /> <!-- [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]: every row -->
<echo value="$prices->cells(columns)" /> <!-- { fruit: ['apple', 'plum'], price: [3, 5] }: every column, by its header -->
<echo value="$prices->cells(row: 0).fruit" /> <!-- 'apple': the header line names each row's keys -->
<echo value="$prices->cells(column: 'price')" /> <!-- [3, 5]: the cells under the price header -->
<echo value="$prices->cells(row: 1, column: 'price')" /> <!-- 5: where plum's row meets the price column -->echo $prices->cells() // [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]: every row
echo $prices->cells(columns) // { fruit: ['apple', 'plum'], price: [3, 5] }: every column, by its header
echo $prices->cells(row: 0).fruit // 'apple': the header line names each row's keys
echo $prices->cells(column: 'price') // [3, 5]: the cells under the price header
echo $prices->cells(row: 1, column: 'price') // 5: where plum's row meets the price columnrow: and rows are one choice, and so are column: and columns: giving both of a pair is an error. One from each pair works together: ->cells(rows, column: 'price') is every row's price, the same as ->cells(column: 'price'). A one row and every column can't go together, because a row's cells are already one a column, so ->cells(row: 0, columns) is an error that says to use ->cells(row: 0).
Cells written as numbers come out as numbers, so they add up straight away. Other cells are text, and so are codes such as 007, whose leading zero would be lost.
The first line is a header unless every cell on it is a number, because then it is data. Puzzle input and readings usually have no header, so every line is a row of numbers. Say header or not header when the first line could be either:
<const name="readings">```csv
387
942
945
```</const>
<echo value="$readings->cells(column: 0)->sum()" /> <!-- 2274: every line is a number, so there's no header -->
<echo value="$prices->cells(not header)->count()" /> <!-- 3: the fruit,price line is a row too -->const $readings = ```csv
387
942
945
```
echo $readings->cells(column: 0)->sum() // 2274: every line is a number, so there's no header
echo $prices->cells(not header)->count() // 3: the fruit,price line is a row tooCells are split by commas. When a csv has no commas, tabs or semicolons split them instead, and so do spaces between numbers. Rows can hold different numbers of cells:
<const name="numbers">```csv
-903 -642 -518 -585 446
692 853
```</const>
<echo value="$numbers->cells(row: 0)" /> <!-- [-903, -642, -518, -585, 446]: spaces split the numbers -->
<echo value="$numbers->cells(row: -1)->sum()" /> <!-- 1545: the last row, 692 + 853 -->
<echo value="$numbers->cells(column: 2)" /> <!-- [-518]: the third number of each row long enough to have one -->
<echo value="$numbers->cells(columns)" /> <!-- [[-903, 692], [-642, 853], [-518], [-585], [446]] -->const $numbers = ```csv
-903 -642 -518 -585 446
692 853
```
echo $numbers->cells(row: 0) // [-903, -642, -518, -585, 446]: spaces split the numbers
echo $numbers->cells(row: -1)->sum() // 1545: the last row, 692 + 853
echo $numbers->cells(column: 2) // [-518]: the third number of each row long enough to have one
echo $numbers->cells(columns) // [[-903, 692], [-642, 853], [-518], [-585], [446]]Without a header, a column skips the rows too short to reach it, so column: 2 gives the third number of every row that has one, and columns gives shorter lists for the later columns. A column no row reaches is an error. Under a header every row has every column, so a short row there is an error.
Give as: a type and the result is checked and turned into it. Rows become records of the type, with their cells turned into its field types:
<type name="Price">
<field name="fruit" type="string" />
<field name="price" type="int" />
</type>
<let name="cheapest" value="$prices->cells(as: Price)->min(by: fn(row) => $row.price)" />
<echo value="$cheapest.fruit" /> <!-- 'apple': 3 is the lowest price -->type Price {
fruit: string
price: int
}
$cheapest = $prices->cells(as: Price)->min(by: fn(row) => $row.price)
echo $cheapest.fruit // 'apple': 3 is the lowest priceIt isn't a file
@read_csv reads a file, so it takes a file_path. Passing it csv text, as in @read_csv($prices), is an error: "The 'path' argument needs a file_path, but this is csv text, not a file." Its quick fix reads the csv itself instead:
<echo value="$prices->cells()" /> <!-- the rows, read from the text itself -->echo $prices->cells() // the rows, read from the text itself->save() writes it to a file, adding .csv when the name has no extension, and @read_csv reads that file back:
<do>$prices->save('prices')</do> <!-- writes prices.csv -->$prices->save('prices') // writes prices.csv