Home Explore Blog CI



nushell

6th chunk of `book/working_with_tables.md`
2ca1ff48f543af7e9d5c633f8c21ebc0468186ce4541f686000000010000139a
# => │  6 │ /lib64 │ symlink │ usr/lib │ rwxrwxrwx │ root │ root  │    7 B │
# => │  7 │ /mnt   │ dir     │         │ rwxr-xr-x │ root │ root  │    0 B │
# => ...
```

### The # Index Column

You've noticed that every table, by default, starts with a column with the heading `#`. This column can operate in one of two modes:

1. Internal #

   - The default mode
   - Nushell provides a 0-based, consecutive index
   - Always corresponds to the cell-path row-number, where `select 0` will return the first item in the list, and `select <n-1>` returns the nth item
   - Is a display of an internal representation only. In other words, it is not accessible by column name. For example, `get index` will not work, nor `get #`

1. "Index"-Renamed #

   - When a column named "index" is created, either directly or as a side-effect of another command, then this `index` column takes the place of the `#` column in the table display. In the table output, the column header is still `#`, but the _name_ of the column is now `index`.

     Example:

     ```nu
     ls | each { insert index { 1000 }} | first 5
     # => ╭──────┬─────────────────┬──────┬─────────┬──────────────╮
     # => │    # │      name       │ type │  size   │   modified   │
     # => ├──────┼─────────────────┼──────┼─────────┼──────────────┤
     # => │ 1000 │ CNAME           │ file │    15 B │ 9 months ago │
     # => │ 1000 │ CONTRIBUTING.md │ file │ 4.3 KiB │ 9 hours ago  │
     # => │ 1000 │ LICENSE         │ file │ 1.0 KiB │ 9 months ago │
     # => │ 1000 │ README.md       │ file │ 2.2 KiB │ 3 weeks ago  │
     # => │ 1000 │ assets          │ dir  │ 4.0 KiB │ 9 months ago │
     # => ╰──────┴─────────────────┴──────┴─────────┴──────────────╯
     ```

     - If an `index` key is added to each row in the table, then it can be accessed via `select` and `get`:

     ```nu
     ls | each { insert index { 1000 }} | first 5 | select index name
     # => ╭──────┬─────────────────╮
     # => │    # │      name       │
     # => ├──────┼─────────────────┤
     # => │ 1000 │ CNAME           │
     # => │ 1000 │ CONTRIBUTING.md │
     # => │ 1000 │ LICENSE         │
     # => │ 1000 │ README.md       │
     # => │ 1000 │ assets          │
     # => ╰──────┴─────────────────╯
     ```

     - On the other hand, if some rows have an `index` key and others don't, the result is no longer a table; it is a `list<any>` due to the different record types:

       ```nu
       ls | upsert 3.index { "--->" } | first 5
       # => ╭──────┬─────────────────┬──────┬─────────┬──────────────╮
       # => │    # │      name       │ type │  size   │   modified   │
       # => ├──────┼─────────────────┼──────┼─────────┼──────────────┤
       # => │    0 │ CNAME           │ file │    15 B │ 9 months ago │
       # => │    1 │ CONTRIBUTING.md │ file │ 4.3 KiB │ 9 hours ago  │
       # => │    2 │ LICENSE         │ file │ 1.0 KiB │ 9 months ago │
       # => │ ---> │ README.md       │ file │ 2.2 KiB │ 3 weeks ago  │
       # => │    4 │ assets          │ dir  │ 4.0 KiB │ 9 months ago │
       # => ╰──────┴─────────────────┴──────┴─────────┴──────────────╯

       ls | upsert 3.index { "--->" } | first 5 | describe
       # => list<any> (stream)

       ls | upsert 3.index { "--->" } | select index name
       # Error: cannot find column 'index'

       ls | upsert 3.index { "--->" } | select index? name | first 5
       # => ╭──────┬─────────────────╮
       # => │    # │      name       │
       # => ├──────┼─────────────────┤
       # => │      │ CNAME           │
       # => │      │ CONTRIBUTING.md │
       # => │      │ LICENSE         │
       # => │ ---> │ README.md       │
       # => │      │ assets          │
       # => ╰──────┴─────────────────╯
       ```

Title: Understanding the # Index Column in Nushell Tables
Summary
This section explains the behavior of the `#` index column in Nushell tables. It operates in two modes: 'Internal #' where Nushell provides a 0-based index (not accessible by column name), and 'Index'-Renamed #' where a column named 'index' replaces the default index, making it accessible via `select` and `get`. The section also details how the presence or absence of the 'index' key in rows affects whether the output remains a table or becomes a list of any type.