Skip to content

? Vector Conditional

Replace selected items of one list with corresponding items of another

?[x;y;z]

Where

  • x is a boolean atom or vector
  • y and z are atoms or lists of the same type
  • x, y, and z conform

returns

  • y if x is a true atom, or z if x is a false atom
  • a list that takes the items of y where x is true and the items of z where x is false, if x is a list

All three arguments are evaluated.

q)?[11001b;1 2 3 4 5;10 20 30 40 50]
1 2 30 40 5

If x, y, or z are atoms, they are repeated. Since V2.7 2010.10.07 ?[x;y;z] works for atoms too.

q)?[11001b;1;10 20 30 40 50]
1 1 30 40 1
q)?[11001b;1 2 3 4 5;99]
1 2 99 99 5
q)?[0b;1;2]
2

x must be boolean. Other types signal a 'type error.

q)?[1 0 1;1 2 3;4 5 6]
'type
  [0]  ?[1 0 1;1 2 3;4 5 6]
       ^

You can use Vector Conditional in qSQL queries, which do not support Cond.

For more than two cases, see the Case iterator.


? Query, Cond, if

Controlling evaluation

Q for Mortals §10.1.3 Vector Conditional Evaluation