/

Table manipulation

This page describes table manipulation functions docs ↗ that allow you to sort, filter, and compute values from any kind of table, including plain Markdown ones and those loaded from CSV.

Example 1

.tablesort {2} order:{descending}
    .csv {assets/people.csv}
UsernameBirth yearFavorite foodFavorite drink
iamgio2002PastaIced tea
john1992ChickenOrange juice
daniel1986SushiBeer

Sort rows

The .tablesort function sorts a table based on the values of a specific column.

ParameterDescriptionAccepts
columnIndex of the column, starting from 1.1 to number of columns.
orderSorting order.ascending (default), descending

Example 2

.tablesort {2} order:{descending}
    | Name | Age | City |
    |------|-----|------|
    | John | 25  | NY   |
    | Lisa | 32  | LA   |
    | Mike | 19  | CHI  |
NameAgeCity
Lisa32LA
John25NY
Mike19CHI

Example 3

The sorting follows the most natural way for humans to sort strings (alphanumeric sorting):

.tablesort {2}
    |   Item   | Price |
    |----------|-------|
    | Pencil   | $1    |
    | Eraser   | $0.50 |
    | Backpack | $20   |
    | Notebook | $3    |
ItemPrice
Eraser$0.50
Pencil$1
Notebook$3
Backpack$20

Filter rows

The .tablefilter function keeps or removes rows based on the values of a specific column.

ParameterDescriptionAccepts
columnIndex of the column, starting from 1.1 to number of columns.
filterLambda that returns whether each row should be kept, with the value of its cell in the corresponding column as input.DynamicBoolean lambda

Example 4

.tablefilter {2} {@lambda x: .x::isgreater {20}}
    | Name | Age | City |
    |------|-----|------|
    | John | 25  | NY   |
    | Lisa | 32  | LA   |
    | Mike | 19  | CHI  |
NameAgeCity
John25NY
Lisa32LA

Compute/aggregate columns

The .tablecompute function computes the cells in a column and appends the result to a new row.

ParameterDescriptionAccepts
columnIndex of the column, starting from 1.1 to number of columns.
computeLambda that returns the computed value, with the collection of the cells in the column as input.IterableDynamic lambda

See Iterable to learn more about available operations on collections.

Example 5

.tablecompute {2} {@lambda x: .x::average::round}
    | Name | Age | City |
    |------|-----|------|
    | John | 25  | NY   |
    | Lisa | 32  | LA   |
    | Mike | 19  | CHI  |
NameAgeCity
John25NY
Lisa32LA
Mike19CHI
25

Composition

You can chain multiple table operations. The order of operations goes from inner to outer:

Example 6

.tablecompute {2} {@lambda x: .x::average::round}
    .tablesort {2}
        | Name | Age | City |
        |------|-----|------|
        | John | 25  | NY   |
        | Lisa | 32  | LA   |
        | Mike | 19  | CHI  |
NameAgeCity
Mike19CHI
John25NY
Lisa32LA
25

Retrieve columns

The .tablecolumn function extracts values from the cells of a specific column and returns them as an Iterable.

ParameterDescriptionAccepts
columnIndex of the column, starting from 1.1 to number of columns.

Example 7

.var {values}
    .tablecolumn {2}
        | Name | Age | City |
        |------|-----|------|
        | John | 25  | NY   |
        | Lisa | 32  | LA   |
        | Mike | 19  | CHI  |

.values::first

25

Bulk-retrieve columns

Additionally, the .tablecolumns function returns all the columns from the table as an iterable of iterables. This is more efficient when you need to access multiple columns from the same table, compared to calling .tablecolumn multiple times.

Example 8

.var {columns}
    .tablecolumns
        | Name | Age | City |
        |------|-----|------|
        | John | 25  | NY   |
        | Lisa | 32  | LA   |
        | Mike | 19  | CHI  |

.foreach {.columns}
    col:
    .col::first

John

25

NY