4

Let's say I have the following two lists:

 x : `a`b`c`d;
 y : `a`b`e`f;

For the intersection, there is the inter operator:

q)x inter y
`a`b

Is there a similar operator to do the EXCLUSIVE OR such that I would get:

q)x outer y
`c`d`e`f

?

JejeBelfort
  • 1,593
  • 2
  • 18
  • 39

2 Answers2

9

the operation except will give you elements of one list that do not belong in another.

However in your case x except y would only give `c`d and y except x would only give `e`f.

Therefore you could use either;

q)(x except y),y except x
`c`d`e`f

or

q)(x union y) except (x inter y)
`c`d`e`f

or alternatively without using except

q)where(count each group (distinct x), distinct y)=1
`c`d`e`f

if you want to get a list of all exclusive elements.

Regards, Kevin

Kevin O'Hare
  • 246
  • 1
  • 4
  • 1
    Nice. You could do away with the `union` and just make it `(x,y)except x inter y` - though it would leave duplicates as mentioned by Callum – terrylynch Jun 26 '19 at 10:42
5

From Kevin's answers, the first will give duplicates if they are present in either, the last will give the distinct list. To use the function infix as you have requested, you need to define the function in the .q namespace

q).q.outer:{(x union y) except (x inter y)}
q)x outer y
`c`d`e`f
Callum Biggs
  • 1,539
  • 5
  • 13