Reputation: 103
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[
ab
c]; 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
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
Reputation: 21757
Assuming what you want is to change this:
To something like this i.e. remove the entries for a, b, and c:
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