Search code examples
kdb

(kdb+/q) How to return a single combined output when looping through two list parameters (one on inner loop/one on outer loop)


I am performing a string search on 929 strings in a list.

I am looping through 7 different strings.

The issue I am having is how to return the output as one and not returning it 7 times for each different string that is searched.

q){{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
..
q)
q)count {{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
7
q)
q)count each {{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
929 929 929 929 929 929 929
q)

Solution

  • Sample data:

    dToCol:([] v:("aa";"cc";"ff"))
    rdeFunc:([] rd:("aa";"bb";"cc";"dd";"ee";"ff"))
    

    Your code gives 3 lists of 6:

    q)count each {{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
    6 6 6
    

    If you wanted 6 lists of 3 you can use flip:

    count each flip {{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
    3 3 3 3 3 3
    

    Or reorder your query to not need the flip:

    count each {ss[x] each exec v from dToCol}each exec rd from rdeFunc
    3 3 3 3 3 3
    

    Or use \:/: each-left each-right to simplify further:

    https://code.kx.com/q/ref/maps/#left-right-cross

    count each ss\:/:[dToCol`v;rdeFunc`rd]
    3 3 3 3 3 3
    

    Verify all results are exact matches:

    r3:ss\:/:[dToCol`v;rdeFunc`rd]
    r2:{ss[x] each exec v from dToCol}each exec rd from rdeFunc
    r1:flip {{ss[x;y]}[;x]each exec rd from rdeFunc}each exec v from dToCol
    r1~r2
    1b
    r2~r3
    1b