Back to Inserting Dictionary Into Table

Dictionaries · Inserting Dictionary Into Table · Practice Question

Append Dictionary to Table

Sign in to track your progress

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.5

A 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       150

Solution

insert[`t;order]
t
orderId qty price
-----------------
1       100 10.5
2       200 11
3       300 11.5
4       150

Alternative Solution

t,((enlist `price)!(enlist 0n)),order

Same 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`.