Home Explore Blog CI



nushell

3rd chunk of `book/navigating_structured_data.md`
b164ac9999460ed21f018427d5f2cd43754d235d6667d6200000000100000e81
# => ╭───┬────────╮
# => │ 0 │ sunny  │
# => │ 1 │ sunny  │
# => │ 2 │ cloudy │
# => │ 3 │ rain   │
# => ╰───┴────────╯
```

#### Example - Access a Table Cell (Value)

The condition for the fourth day:

```nu
$data.condition.3
# => rain
```

### Nested Data

Since data can be nested, a cell-path can contain references to multiple names or indices.

#### Example - Accessing Nested Table Data

To obtain the temperature at the second weather station on the third day:

```nu
$data.temps.2.1
# => 36.67
```

The first index `2` accesses the third day, then the next index `1` accesses the second weather station's temperature reading.

## Using `get` and `select`

In addition to the cell-path literal syntax used above, Nushell also provides several commands that utilize cell-paths. The most important of these are:

- `get` is equivalent to using a cell-path literal but with support for variable names and expressions. `get`, like the cell-path examples above, returns the **value** indicated by the cell-path.
- `select` is subtly, but critically, different. It returns the specified **data structure** itself, rather than just its value.
  - Using `select` on a table will return a table of equal or lesser size
  - Using `select` on a list will return a list of equal or lesser size
  - using `select` on a record will return a record of equal or lesser size

Continuing with the sample table above:

### Example - `get` vs. `select` a table row

```nu
$data | get 1
# => ╭───────────┬───────────────╮
# => │ date      │ 2 years ago   │
# => │           │ ╭───┬───────╮ │
# => │ temps     │ │ 0 │ 35.24 │ │
# => │           │ │ 1 │ 35.94 │ │
# => │           │ │ 2 │ 34.91 │ │
# => │           │ │ 3 │ 35.24 │ │
# => │           │ │ 4 │ 36.65 │ │
# => │           │ ╰───┴───────╯ │
# => │ condition │ sunny         │
# => ╰───────────┴───────────────╯

$data | select 1
# => ╭───┬─────────────┬───────────────┬───────────╮
# => │ # │    date     │     temps     │ condition │
# => ├───┼─────────────┼───────────────┼───────────┤
# => │ 0 │ 2 years ago │ ╭───┬───────╮ │ sunny     │
# => │   │             │ │ 0 │ 35.24 │ │           │
# => │   │             │ │ 1 │ 35.94 │ │           │
# => │   │             │ │ 2 │ 34.91 │ │           │
# => │   │             │ │ 3 │ 35.24 │ │           │
# => │   │             │ │ 4 │ 36.65 │ │           │
# => │   │             │ ╰───┴───────╯ │           │
# => ╰───┴─────────────┴───────────────┴───────────╯
```

Notice that:

- [`get`](/commands/docs/get.md) returns the same record as the `$data.1` example above
- [`select`](/commands/docs/select.md) returns a new, single-row table, including column names and row indices

::: tip
The row indices of the table resulting from `select` are not the same as that of the original. The new table has its own, 0-based index.

To obtain the original index, you can use the [`enumerate`](/commands/docs/enumerate.md) command. For example:

Title: Using `get` and `select` for Data Access in Nushell
Summary
This section contrasts the `get` and `select` commands in Nushell, both of which use cell-paths but return different types of data. `get` returns the value indicated by the cell-path, while `select` returns the data structure (table, list, or record) itself. An example demonstrates the difference when accessing a table row: `get` returns the record, while `select` returns a new, single-row table. The section also notes that the row indices in the resulting table from `select` are 0-based and can be accessed using the `enumerate` command.