Querying a column that holds a dictionary

Hi.I have a table where one column is a dictionary. An example:t:([id: 1 2] col1:(10 20); dictcol:((akey1akey2!100 100);(akey1akey2!200 300)))I would then like to do something like:select id from t where dictcol[`akey]>150but this doesn’t work as I would’ve expected it to work. I don’treally understand why, or what I can do about it.Thanks.

I think this is what you want:

q)select id from t where dictcol[;`akey1]>150
id

2

Not sure why you want this, but you might want to use exec instead of
select. Just a guess though.

q)exec id from t where dictcol[;`akey1]>150
,2

Thanks a lot!Could you elaborate on why that works? I want to have a betterunderstanding (just started learning q) so I won’t have to ask againif a similar problem arises.Optimally, I want to have a custom function in the where phrase like:select something from atable where myfunc[column, somearg]where myfunc returns a boolean. I can do this fine when the columnjust contains integers or something. But in my scenario I want it towork for dictionaries to, but I can’t get it to work for the samereason I failed with the example in the original post. Can you make itunderstandable somehow?On 26 mar, 13:30, Tim Rieder <trie…> wrote:> I think this is what you want:>> q)select id from t where dictcol[;akey1]&gt;150&gt; id&gt; --&gt; 2&gt;&gt; Not sure why you want this, but you might want to use exec instead of&gt; select. Just a guess though.&gt;&gt; q)exec id from t where dictcol[;akey1]>150> ,2>>>> On Sat, Mar 26, 2011 at 7:06 AM, uman <mikaelu…> wrote:> > Hi.>> > I have a table where one column is a dictionary. An example:>> > t:([id: 1 2] col1:(10 20); dictcol:((akey1akey2!100 100);> > (akey1akey2!200 300)))>> > I would then like to do something like:>> > select id from t where dictcol[`akey]>150>> > but this doesn’t work as I would’ve expected it to work. I don’t> > really understand why, or what I can do about it.>> > Thanks.>> > –> >

Submitted via Google Groups</mikaelu…></trie…>

q has some interesting quirks when it sees a list of dictionaries. Alist of dictionaries with the same domain are automatically convertedinto a table:q)t1:enlist ab!1 2q)t1a b—1 2q)type t198hA list of dictionaries with different domains is left as a list:q)l1:(ab!1 2;ab!2 3;ac!2 3)q)l1ab!1 2ab!2 3ac!2 3q)type l10hq)type 2_l198hThe dictionary data in the original post is treated similarly:q)dc:((akey1akey2!100 100);(akey1akey2!200 300))q)dcakey1 akey2-----------100 100200 300q)type dc98hq)t:([id:1 2];col1:10 20;dictcol:dc)q)tid| col1 dictcol–| -------------------------1 | 10 akey1akey2!100 1002 | 20 akey1akey2!200 300The strange part is that the dictcol “table” is converted back intolist form when it appears inside another table (in this case, thetable that holds the values of t).q)(value t)@dictcolakey1 akey2-----------100 100200 300q)type (value t)@dictcol0hI don’t think this ambiguity of representation has much bearing on theoriginal post, but it’s something to keep in mind if you’re workingwith lists of dictionaries.