CSV
csvの値とは、有効なCSVを含むテキストのことです。CSVは、カンマで区切られたセルの行で構成され、1行に1つの行があります。```csvブロックとして記述するか、変数にcsv型と引用符付きのテキストを与えてください。いずれの場合も、CSVとして読めない行(例えば閉じられていない引用符)は、記述された場所でエラーとなります。エディタ上でも、スクリプトを実行する際にもエラーになります。
<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'通常、最初の行には列の名前が書かれています。.headers はそれらを読み取ります:
<echo value="$prices.headers" /> <!-- ['fruit', 'price']: the first line names the columns -->echo $prices.headers // ['fruit', 'price']: the first line names the columnsセルの読み取り
->cells() は CSV のセルを読み取ります。単独で使用するか、rows フラグを付けると全行を返します。columns を付けると全列を返します。row: は 1 行を、column: は 1 列を選択します。行は 0 から数えます(-1 が最後の行です)。列はヘッダー名で指定するか、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:とrowsは択一です。column:とcolumnsも同様に択一です。どちらかのペアを両方指定するとエラーになります。各ペアから1つずつ選ぶことは可能です。例えば、->cells(rows, column: 'price')は全行の価格を表し、->cells(column: 'price')と同じ意味になります。1行の指定と全列の指定は一緒に使えません。行のセルは既に列の1つになっているため、->cells(row: 0, columns)はエラーになり、代わりに->cells(row: 0)を使うよう示されます。
数値として入力されたセルは数値として扱われるため、そのまま合計できます。その他のセルはテキストとして扱われます。先頭のゼロが失われてしまうため、007のようなコードもテキストになります。
最初の行は、すべてのセルが数値でない限りヘッダーとして扱われます。すべてのセルが数値の場合は、データとして扱われます。パズルの入力や測定値には通常ヘッダーがないため、すべての行が数値の行になります。最初の行がどちらにも解釈できる場合は、header または not header と指定してください:
<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 tooセルはカンマで区切られます。CSVにカンマがない場合は、代わりにタブまたはセミコロンで区切られます。数字の間のスペースも区切りとして扱われます。行ごとに異なる数のセルを持つことができます:
<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]]ヘッダーがない場合、列は短すぎて届かない行をスキップします。そのため column: 2 は、3番目の数値を持つすべての行の3番目の数値を返し、columns は後の列ほど短いリストを返します。どの行も届かない列はエラーになります。ヘッダーがある場合、すべての行にすべての列があるため、そこで行が短いとエラーになります。
as: に型を指定すると、結果がチェックされてその型に変換されます。行は型のレコードになり、そのセルは各フィールドの型に変換されます:
<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 priceファイルではありません
@read_csv はファイルを読み込むため、file_path を受け取ります。@read_csv($prices) のように CSV テキストを渡すとエラーになります:"'path' 引数には file_path が必要ですが、これは CSV テキストでありファイルではありません。" 簡単な修正方法として、代わりに CSV を直接読み込みます:
<echo value="$prices->cells()" /> <!-- the rows, read from the text itself -->echo $prices->cells() // the rows, read from the text itself->save() はそれをファイルに書き込みます。名前に拡張子がない場合は .csv を追加し、@read_csv はそのファイルを読み戻します:
<do>$prices->save('prices')</do> <!-- writes prices.csv -->$prices->save('prices') // writes prices.csv