Vortex
Vortex

Reputation: 789

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

Upvotes: 3

Views: 1510

Answers (2)

WooiKent Lee
WooiKent Lee

Reputation: 1311

I think you should use enlist to drop the string key from dictionary:

q)enlist["k1"]_("k1";"k2";"ABC")!("v1";"v2";"BLA BLA")
"k2" | "v2"
"ABC"| "BLA BLA"

_(drop) takes left input as list of elements of the dictionary key (with exception when it comes to symbol key). Try to imagine that in your case, "k1" is an 'atom' and to create a singleton list, you can just enlist "k1".

Reference: http://code.kx.com/q/ref/lists/#_-cut

Upvotes: 5

Rahul
Rahul

Reputation: 3969

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

Upvotes: 3

Related Questions