nushell

expand collapsed data in nushell


I'm trying to learn nushell, and from this example in the documentation,

> echo [[column1, column2]; [Value1, Value2]]
───┬─────────┬─────────
 # │ column1 │ column2
───┼─────────┼─────────
 0 │ Value1  │ Value2
───┴─────────┴─────────

but my output is:

echo [[column1, column2], [Value1, Value2]]  
╭───┬────────────────╮
│ 0 │ [list 2 items] │
│ 1 │ [list 2 items] │
╰───┴────────────────╯

How do you expand the collapsed information?


Solution

  • It sounds like what you really want to do is convert a Nushell list of lists to a table (a list of records). I'm a bit surprised myself that there isn't (at least that I could find), an easier way (i.e. a built-in command) to do this coercion, but there are several ways to accomplish it. However, I still feel that there's an easier way that I'm just not seeing myself.

    Anyway, the easiest that I could come up with, for your particular example:

    〉[[column1, column2], [Value1, Value2]] | each { { 0: $in.0, 1: $in.1 }} | headers
    ╭───┬─────────┬─────────╮
    │ # │ column1 │ column2 │
    ├───┼─────────┼─────────┤
    │ 0 │ Value1  │ Value2  │
    ╰───┴─────────┴─────────╯
    

    Note, in most cases, you can drop the echo commands and have exactly the same results.

    Also note that there may be a simpler answer by transforming the data earlier, but you don't mention the source of the data. It's unlikely that the original source of the data would be a Nushell "list literal" in the "real world". But for purely academic/learning purposes, it's certainly worth understanding.


    Explanation:

    The first data type you have above (from the doc example) is truly a Nushell table:

    〉[[column1, column2]; [Value1, Value2]] | describe
    table<column1: string, column2: string>
    

    The second is a list of lists:

    〉 [[column1, column2], [Value1, Value2]] | describe
    list<list<string>>
    

    Also note that the doc example above might be considered a bit outdated. When it was written, it was the only way to construct a table-literal in Nushell (and is still probably considered the "table-literal" syntax). But the addition of the record-syntax in 0.40 brought us a new way to specify a table literal, as a "list of records". You can now use the (IMHO) more readable (or at least "standard"):

    [{ 'column1':'value1', 'column2':'value2' }]
    

    That said, the [[column1, column2]; [Value1, Value2]] form is more concise if you ever really needed to specify multiple rows for the table. But whether or not this is a "real world" use is another question, and it wouldn't surprise me to see the "old" table-literal syntax removed before Nushell reaches 1.0.

    It's that new "list-of-records" form that we use to answer your question:

    [[column1, column2], [Value1, Value2]] | each { { 0: $in.0, 1: $in.1 }} | headers
    

    Bonus:

    The following will display lists with an arbitrary number of values:

    [[Column1, Column2, Column3 ], [Value1, Value2], [Value3, Value4, Value5], [Value6]] |
        each { |values|
            reduce -f {} { |it,acc|
                $acc | insert $"Field($acc | transpose | length)" $it
            }
        } | headers
    

    Result:

    ╭───┬─────────┬─────────┬─────────╮
    │ # │ Column1 │ Column2 │ Column3 │
    ├───┼─────────┼─────────┼─────────┤
    │ 0 │ Value1  │ Value2  │         │
    │ 1 │ Value3  │ Value4  │ Value5  │
    │ 2 │ Value6  │         │         │
    ╰───┴─────────┴─────────┴─────────╯
    

    Explanation (in case you aren't familiar with reduce yet):