Dictionaries · Inserting Dictionary Into Table · Practice Question
Append Dictionary to Table
Append a Dictionary to a Table
Question
A trading system stores orders in the following table:
t:([] orderId:1 2 3; qty:100 200 300; price:10.5 11.0 11.5)Result:
orderId qty price
-----------------
1 100 10.5
2 200 11
3 300 11.5A new order arrives as a dictionary:
order:`orderId`qty!(4;150)Notice that the price field is missing.
Append the order to the table, populating any missing columns with null values.
Expected result:
orderId qty price
-----------------
1 100 10.5
2 200 11
3 300 11.5
4 150Solution
insert[`t;order]
torderId qty price
-----------------
1 100 10.5
2 200 11
3 300 11.5
4 150Alternative Solution
t,((enlist `price)!(enlist 0n)),orderSame result. (Run this on the original three-row t, not after the insert above.)
Explanation
insert matches order's keys (orderId, qty) to t's columns by name and fills the missing price column with a float null, 0n (shown as a blank in the console). The Join alternative builds that explicitly: (enlist price)!(enlist 0n)supplies the missing column, joining it withordergives a dictionary covering all three columns, which is then joined ontot`.