Chechy Levas
Chechy Levas

Reputation: 2312

Merge 2 lists according to values in a boolean list

I have a method of achieving this which also explains my question.

a:1 2 3 4;
b:5 6 7;
cond:1101001b;
comb:(count cond) # 0N;
comb[where cond]:a;
comb[where not cond]:b

But q has so many utilities for manipulating lists, I am wondering if there is a more direct way of doing this.

Upvotes: 0

Views: 167

Answers (2)

SJT
SJT

Reputation: 1097

rank is what you need.

q)comb
1 2 5 3 6 7 4
q)(b,a)rank cond
1 2 5 3 6 7 4

Upvotes: 6

Cathan Gormley
Cathan Gormley

Reputation: 61

You could write the expression in a single line

comb:@[;where not cond;:;b] @[;where cond;:;a] (count cond)#0N

Alternatively, assuming the 1s and 0s of cond matches the lengths of a and b:

(a,b) iasc where[cond],where not cond

Upvotes: 2

Related Questions