2020.10.12 07:55 AM
2020.10.12 08:11 AM
q)d1:`a`b`c! (1 2; 3 4 5; 6 7 8 9)
q)where 5 in/:d1
,`b
q)d1:`a`b`c! (1;2;3)
q)d1?2
`b
2020.10.12 08:28 AM
2020.10.12 12:49 PM
Hi!
When looking up if a simple dictionary includes a certain value you can use a technique known as Reverse Lookup using the ? (find) operator. See example 1. This will return the key that particular value is stored under.
Ex 1:q)d: 1 2 3 4!5 6 7 8q)d1| 52| 63| 74| 8q)d?51q)d?62
When using Reverse Lookup in a non-simple dictionary the find (?) operator won't work without the entire list of values for a particular key. See example 2.Ex 2:q)d:(`a`b; `c`d`e; enlist `f)!10 20 30q)d?20`c`d`eq)d:`a`b`c!(10 20; 30 40 50; enlist 60)q)d `b30 40 50q)d?30 40 50`bq)d?40`In a nested dictionary we can use a mixture of the in operator and the each both iterator to determine the keys and values that contain a boolean true. See example 3.Ex 3:q)5 in'd1| 02| 13| 04| 1q)d where 1=5 in'd4 5 65 6 7
Thanks.Caitlin
2020.10.12 11:37 PM
2020.10.12 08:29 AM
Hi Geraldo,
One solution using each-right syntax (/:)q)d1:`a`b`c! (1 2; 3 4 5; 6 7 8 9)
q)where 5 in/:d1
,`bFor simpler dictionary structures (where the values are atomic) we can use a reverse lookup:q)d1:`a`b`c! (1;2;3)
q)d1?2
`bThanksConnorᐧ
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.