Home Explore Blog CI



nushell

7th chunk of `book/working_with_tables.md`
a7267b9057f263a70ec74c34aba2a3c4decafb30830791120000000100000dc7
       # => │    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          │
       # => ╰──────┴─────────────────╯
       ```

   - As demonstrated in the example above, any rows (records) in the table without an `index` key will continue to display the internal representation.

#### Additional Index Examples

##### Convert # to Index

A useful pattern for converting an internal `#` into an index for all rows, while maintaining the original numbering, is:

```nu
ls | enumerate | flatten
```

While the results _look_ the same, the `index` is now decoupled from the internal cell-path. For example:

```nu
ls | enumerate | flatten | sort-by modified | first 5
# => ╭────┬──────────────┬──────┬─────────┬──────────────╮
# => │  # │     name     │ type │  size   │   modified   │
# => ├────┼──────────────┼──────┼─────────┼──────────────┤
# => │  0 │ CNAME        │ file │    15 B │ 9 months ago │
# => │  2 │ LICENSE      │ file │ 1.0 KiB │ 9 months ago │
# => │  4 │ assets       │ dir  │ 4.0 KiB │ 9 months ago │
# => │ 17 │ lefthook.yml │ file │ 1.1 KiB │ 9 months ago │
# => │ 24 │ snippets     │ dir  │ 4.0 KiB │ 9 months ago │
# => ╰────┴──────────────┴──────┴─────────┴──────────────╯

ls | enumerate | flatten | sort-by modified | select 4
# => ╭────┬──────────┬──────┬─────────┬──────────────╮
# => │  # │   name   │ type │  size   │   modified   │
# => ├────┼──────────┼──────┼─────────┼──────────────┤
# => │ 24 │ snippets │ dir  │ 4.0 KiB │ 9 months ago │
# => ╰────┴──────────┴──────┴─────────┴──────────────╯

Title: Further Examples and Conversion of the # Index in Nushell
Summary
This section provides additional examples of how the `#` index column behaves in Nushell when some rows lack the `index` key. It demonstrates that such tables are treated as a `list<any>` rather than a regular table. Furthermore, it introduces a pattern using `enumerate` and `flatten` to convert the internal `#` into a user-accessible `index` column while preserving the original numbering, decoupling the index from the internal cell-path.