Slide 22 of 74

Tables and CSV

Rows, columns and cuts
proc main(): void {
	Table crew = [["name", "role"], ["ada", "engineer"], ["grace", "admiral"]]
	echo row(crew, "ada")                          // ["ada", "engineer"]
	echo column(crew, "role")                      // ["role", "engineer", "admiral"]
	echo toTable(["a", "b", "c", "d", "e"], 2)     // [["a", "b"], ["c", "d"], ["e"]]
}
Reading a CSV
proc main(): void {
	hive.file.write("./crew.csv", "name,role\nada,\"engineer, first\"\ngrace\n")

	read := hive.file.csv("./crew.csv", ",")
	if read is Result.Ok(table) {
		for each line in table {
			echo "{len(line)} cells: {line}"     // the last row has one
		}
	} else if read is Result.Error(why) {
		echo "{why.path}: {why.message}"
	}
}

Table is the name for Str[dyn][dyn]: a vector of rows of cells. Three builtins work on one:

  • row(table, key) is the first row whose first cell is key, and column(table, key) the column whose top cell is key — each [] when there is none.
  • toTable(cells, wide) cuts a flat vector into rows of wide, the last row holding what was left.

hive.file.csv(path, separator) reads a separated file into one. The separator is always written — there are no optional parameters in Hive — and it is one character. A quoted field may hold the separator, a newline or a doubled ""; blank lines are skipped; a byte-order mark at the start, which spreadsheets often write, is dropped; and every row keeps the length it has, so a ragged file is a table of ragged rows and a cell still needs its guard. A failure is a hive.file.TableError, carrying the path and a message.

Spreadsheets, databases and maps answer with tables too, each on its own slide.