cancel
Showing results for
Did you mean:

## First day

New Contributor
Good Morning!
I just got to know kdb + and the q language.
I am reading and practicing based on the website https://code.kx.com/q4m3/.

I just made several examples with lists.
Now I'm in the dictionaries.

Example:
d1: `a`b`c! (1 2; 3 4 5; 6 7 8 9)

Doubt:
How to know if the dictionary contains a certain value?
Ex.: d1 contains 5?

I thank the attention.
5 REPLIES 5
New Contributor II
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
,`b

For 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
`b

Thanks
Connor

New Contributor
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 8
q)d
1| 5
2| 6
3| 7
4| 8
q)d?5
1
q)d?6
2

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 30
q)d?20
`c`d`e
q)d:`a`b`c!(10 20; 30 40 50; enlist 60)
q)d `b
30 40 50
q)d?30 40 50
`b
q)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'd
1| 0
2| 1
3| 0
4| 1
q)d where 1=5 in'd
4 5 6
5 6 7

Thanks.
Caitlin

New Contributor
Caitlin, thank you very much!

As I said, today is my first day with kdb + e q. Many news.

With your explanation and Connor's explanation, I started to understand better how the q language works.

A big news for me:
q code is has no order of precedence, it is interpreted from right to left.

Note:
q) 5 in`d
q) d where 1 = 5 in'd
presented error.

Look how it worked for me:
q) 5 in /: d
q) where 5 in: / d
q) d where 5 in: / d

Thank you very much for the tips and suggestions.

A big hug to everyone!

Em segunda-feira, 12 de outubro de 2020 às 12:40:28 UTC-3, caitlin...@aquaq.co.uk escreveu:
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 8
q)d
1| 5
2| 6
3| 7
4| 8
q)d?5
1
q)d?6
2

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 30
q)d?20
`c`d`e
q)d:`a`b`c!(10 20; 30 40 50; enlist 60)
q)d `b
30 40 50
q)d?30 40 50
`b
q)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'd
1| 0
2| 1
3| 0
4| 1
q)d where 1=5 in'd
4 5 6
5 6 7

Thanks.
Caitlin

Contributor III
Hi Geraldo

We know code.kx.com is a key resource for solo students of q. For example, your insight about precedence appears at

Much could and should be done to improve the site as a resource for solo study. However, when people study alone it is hard for us to see where difficulties need removing.

Any comments or observations as you learn would be most welcome! (This goes for everyone in this group.)

Best
Stephen

Stephen Taylor | Librarian | Kx | +44 7713 400852 | stephen@kx.com
My job is to put information where people can find it.

New Contributor
Thank you Connor!

For simple dictionaries I had succeeded.
But I thought of a slightly more "advanced" dictionary.

I had tried to use where and each, but it went wrong.

As today is my first day, I still didn't know /: (each right)
Fantastic!

Now I will try to change the value found.
Eg: change from 5 to 8.

A big hug!
Thank you!

Em segunda-feira, 12 de outubro de 2020 às 12:14:44 UTC-3, cge...@kx.com escreveu:
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
,`b

For 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
`b

Thanks
Connor