Advanced table workflows
Merging tables of different size
Examples shown in Working with tables
work fine when our tables have equal amount of rows but what if we want to merge tables of different sizes?
let first_table = [[a b]; [1 2] [3 4]]
let second_table = [[c d]; [5 6]]
$first_table | merge $second_table
Output:
───┬───┬───┬───┬───
# │ a │ b │ c │ d
───┼───┼───┼───┼───
0 │ 1 │ 2 │ 5 │ 6
───┼───┼───┼───┼───
1 │ 3 │ 4 │ ❎│ ❎
───┴───┴───┴───┴───
Columns c
and d
in the second row are empty because our second
table only contained a single row; Nushell has nothing to fill the remaining rows with. But what if we wanted the smaller table to 'wrap around' and keep filling the rows? For that we can use the chunks
command to split the larger table into subtables, merge each of them with the smaller table and then combine the merged tables together using flatten
command
For example:
let first_table = [[a b]; [1 2] [3 4]]
let second_table = [[c d]; [5 6]]
$first_table
| chunks ($second_table | length)
| each { merge $second_table }
| flatten
Output:
───┬───┬───┬───┬───
# │ a │ b │ c │ d
───┼───┼───┼───┼───
0 │ 1 │ 2 │ 5 │ 6
───┼───┼───┼───┼───
1 │ 3 │ 4 │ 5 │ 6
───┴───┴───┴───┴───
Can we do that with more than two tables? Sure we can! Let's add a third table:
let third_table = [[e f]; [7 8]]
We can merge all three tables like this:
$first_table
| chunks ($second_table | length)
| each { merge $second_table }
| flatten
| chunks ($third_table | length)
| each { merge $third_table }
| flatten
Output:
───┬───┬───┬───┬───┬───┬───
# │ a │ b │ c │ d │ e │ f
───┼───┼───┼───┼───┼───┼───
0 │ 1 │ 2 │ 5 │ 6 │ 7 │ 8
───┼───┼───┼───┼───┼───┼───
1 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8
───┴───┴───┴───┴───┴───┴───
Or as mentioned in the Cookbook we can use the reduce
command to merge tables together recursively:
[$first_table $second_table $third_table]
| reduce { |elt, acc|
$acc
| chunks ($elt | length)
| each { merge $elt }
| flatten
}
Output:
───┬───┬───┬───┬───┬───┬───
# │ a │ b │ c │ d │ e │ f
───┼───┼───┼───┼───┼───┼───
0 │ 1 │ 2 │ 5 │ 6 │ 7 │ 8
───┼───┼───┼───┼───┼───┼───
1 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8
───┴───┴───┴───┴───┴───┴───