Search code examples
kdb

kdb how to remove string key from dictionary


I am having difficulty removing an entry from KDB dictionary. Keys and values are strings.

Working

q)l3:`a`b`c!1 2 3
q)`a _l3
b| 2
c| 3

Not working

q)l2:("k1";"k2";"ABC")!("v1";"v2";"BLA BLA")
q)"k1" _l2
'type

Thanks, Eugene


Solution

  • Since type of "key list" in your dictionary is 0h (mixed list or list of list)

         q) type ("k1";"k2";"ABC")
         q) 0h
    

    and type of your single key is 10h (string)

         q)  type "k1"
         q) 10h
    

    Thats why on matching, kdb gives you type error.
    Reference: http://code.kx.com/q4m3/5_Dictionaries/

    It says: "The left operand of delete is the dictionary (target) and the right operand is a key value whose type matches that of target."

    You can use following to remove the entry:

          q)   (k@where not (k:key l2) like "k1")#l2
    
    Key Value
    k2  v2
    ABC BLA BLA