Axiom Lemma
Axiom Lemma

Reputation: 103

Drop a list of keys from a nested table in kdb

I am trying to drop a set of items from a nested table; for instance given the following table

tree:{[t;col]
    if[1=count col; :![t;();0b;enlist[`name]!col]];
    f:first col;
    c:cols[t] except f;
    r:1_col;
    :?[t;();(enlist `name)!enlist f ;`val`child!((sum;`val);({[r;c;l] tree[flip c!l;r]};enlist r;enlist c;(enlist, c )))]
   };

t:flip`cat`dog`caps`val`val1!flip((cross/)(0 1;`$(5 1)#.Q.a;`$(5 1)#.Q.A;til 5;5+til 5));

// derive tree from aforementioned table
tre:tree[t;`cat`dog`caps]; 

// notably returns correct result indexed by name
.[tre;(0;`child)] enlist[`name]!enlist[`a`b`c]; 

// does not work ???
.[tre;(0;`child);_;enlist[`name]!enlist[`a`b`c]]; 

I have thought of using the following as a solution

.[tre;(0;`child);{delete from y where name in x}[`a`b`c]];

I just wanted to check if there is a more concise (genereally accepted) method. Such as using the enlist[name]!enlist[abc]; index to achieve this.

In KDB+ How should one appropriately drop a given set of indices from a nested tree? Thanks for your guidance.

Upvotes: 1

Views: 211

Answers (2)

terrylynch
terrylynch

Reputation: 13572

I like shree.pat's solution! Just adding that your original attempt could work if you modified it slightly:

q).[tre;(0;`child);{y _ x};flip enlist[`name]!enlist[`a`b`c]]
name| val  child                                                             ..
----| -----------------------------------------------------------------------..
0   | 1250 (+(,`name)!,`d`e)!+`val`child!(250 250;(+`caps`val`val1`name!(`A`A..
1   | 1250 (`s#+(,`name)!,`s#`a`b`c`d`e)!+`val`child!(250 250 250 250 250;(+`..

Upvotes: 3

shree.pat18
shree.pat18

Reputation: 21757

Assuming what you want is to change this:

enter image description here

To something like this i.e. remove the entries for a, b, and c: enter image description here

You could amend your code a little bit to use Over like so:

.[tre;(0;`child);_/;`a`b`c]   

This will apply the delete over each of these keys as you want.

Upvotes: 3

Related Questions