在本章中,我们将学习如何操作字典和表格.让我们从词典开始减去;
q)d:`u`v`x`y`z! 9 18 27 36 45 / Creating a dictionary dq)/ key of this dictionary (d) is given byq)key d`u`v`x`y`zq)/and the value byq)value d9 18 27 36 45q)/a specific valueq)d`x27q)d[`x]27q)/values can be manipulated by using the arithmetic operator +-*% as,q)45 + d[`x`y]72 81
如果需要修改字典值,那么修改公式可以是 :
q)@[`d;`z;*;9]`dq)du | 9v | 18x | 27y | 36q)/Example, table tabq)tab:([]sym:`;time:0#0nt;price:0n;size:0N)q)n:10;sym:`IBM`SAMSUNG`APPLE`MSFTq)insert[`tab;(n?sym;("t"$.z.Z);n?100.0;n?100)]0 1 2 3 4 5 6 7 8 9q)`time xasc `tab`tabq)/ to get particular column from table tabq)tab[`size]12 10 1 90 73 90 43 90 84 63q)tab[`size]+921 19 10 99 82 99 52 99 93 72z | 405q)/Example table tabq)tab:([]sym:`;time:0#0nt;price:0n;size:0N)q)n:10;sym:`IBM`SAMSUNG`APPLE`MSFTq)insert[`tab;(n?sym;("t"$.z.Z);n?100.0;n?100)]0 1 2 3 4 5 6 7 8 9q)`time xasc `tab`tabq)/ to get particular column from table tabq)tab[`size]12 10 1 90 73 90 43 90 84 63q)tab[`size]+921 19 10 99 82 99 52 99 93 72q)/Example table tabq)tab:([]sym:`;time:0#0nt;price:0n;size:0N)q)n:10;sym:`IBM`SAMSUNG`APPLE`MSFTq)insert[`tab;(n?sym;("t"$.z.Z);n?100.0;n?100)]0 1 2 3 4 5 6 7 8 9q)`time xasc `tab`tabq)/ to get particular column from table tabq)tab[`size]12 10 1 90 73 90 43 90 84 63q)tab[`size]+921 19 10 99 82 99 52 99 93 72q)/We can also use the @ amend tooq)@[tab;`price;-;2] sym time price size-------------------------------------------- APPLE 11:16:39.779 6.388858 12 MSFT 11:16:39.779 17.59907 10 IBM 11:16:39.779 35.5638 1 SAMSUNG 11:16:39.779 59.37452 90 APPLE 11:16:39.779 50.94808 73 SAMSUNG 11:16:39.779 67.16099 90 APPLE 11:16:39.779 20.96615 43 SAMSUNG 11:16:39.779 67.19531 90 IBM 11:16:39.779 45.07883 84 IBM 11:16:39.779 61.46716 63q)/if the table is keyedq)tab1:`sym xkey tab[0 1 2 3 4]q)tab1 sym | time price size--------- | ---------------------------------- APPLE | 11:16:39.779 8.388858 12 MSFT | 11:16:39.779 19.59907 10 IBM | 11:16:39.779 37.5638 1 SAMSUNG | 11:16:39.779 61.37452 90 APPLE | 11:16:39.779 52.94808 73q)/To work on specific column, try thisq){tab1[x]`size} each sym1 90 12 10q)(0!tab1)`size12 10 1 90 73q)/once we got unkeyed table, manipulation is easyq)2+ (0!tab1)`size14 12 3 92 75