2

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?

1 Answers1

3

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
  • For each pair (e.g. [Value1, Value2]) in the list, creates a record with the first value set as key 0 and the second set as key 1.

  • The results from each are automatically collected into a list.

  • Because the end result is a "list of records where every record is the same type", it results in a table. If one of the rows was different (e.g. it had an integer instead of a string), then the results would look the same, but the actual output would be a "list of records".

  • The headers command is used to take your heading names and use them instead of 0 and 1.

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):

  • Loops over each (inner) list of values in the (outer) list
  • "Reduces" each of those inner lists of values to a record:
    • Starts with an empty record -f {} as $acc
    • Inserts each field as Field0, Field1, and so on.
    • The results are appended to the $acc which becomes the complete record at the end of the reduction.
    • The | transpose | length is a hacky workaround due to a bug that I need to file. I'll link it back here when I do.
NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70