2023.06.07 07:44 AM - edited 2023.06.07 07:45 AM
Hi, I have a simple question:
I have a list b:(1 5 6;9 10 76; 43 12 11) and I would like to take the nth indexed element from each list where n is a list, for example n = (0;2;1).
So the output should be (1;76;12).
What is the easiest syntax to do it? Thanks.
2023.06.07 08:27 AM
q)b@'(0;2;1)
1 76 12
2023.06.07 07:50 AM - edited 2023.06.07 07:52 AM
Here's one option:
q)b:(1 5 6;9 10 76; 43 12 11)
q)n:(0;2;1)
q){x y}'[b;n]
1 76 12
2023.06.07 08:27 AM
q)b@'(0;2;1)
1 76 12
2023.06.07 08:19 AM
As far as I know it's not possible without some kind of each, so here's the same approach for a table:
q)tab:([time:10:03:54.347 10:04:05.827]price:(20.83 21.44 26.83 29.83e;88.88 88.75 83.27 823.77f);n:0 1)
q)update m:{x y}'[price;n] from tab
time | price n m
------------| ---------------------------------
10:03:54.347| 20.83 21.44 26.83 29.83 0 20.83e
10:04:05.827| 88.88 88.75 83.27 823.77 1 88.75
2023.06.07 04:09 PM - edited 2023.06.07 05:46 PM
The lambda isn't necessary here:
q)update m:price@'n from tab
time | price n m
------------| ---------------------------------
10:03:54.347| 20.83 21.44 26.83 29.83 0 20.83e
10:04:05.827| 88.88 88.75 83.27 823.77 1 88.75
Not useful for the general case, but here you can forgo the explicit n column and use the virtual index column instead:
q)tab:([time:10:03:54.347 10:04:05.827]price:(20.83 21.44 26.83 29.83e;88.88 88.75 83.27 823.77f))
q)update m:price@'i from tab
time | price m
------------| -------------------------------
10:03:54.347| 20.83 21.44 26.83 29.83 20.83e
10:04:05.827| 88.88 88.75 83.27 823.77 88.75
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.