Skip to content

, ,: Join and Append

, Join

Join atoms, lists, dictionaries or tables

x,y    ,[x;y]

Where x and y are atoms, lists, dictionaries or tables returns x joined to y.

q)1 2 3,4
1 2 3 4
q)1 2,3 4
1 2 3 4
q)(0;1 2.5;01b),(`a;"abc")
(0;1.00 2.50;01b;`a;"abc")

The result is a vector if both arguments are vectors or atoms of the same type; otherwise a mixed list.

q)1 2.4 5,-7.9 10               /float vectors
1.00 2.40 5.00 -7.90 10.00
q)1 2.4 5,-7.9                  /float vector and atom
1.00 2.40 5.00 -7.90
q)1 2.4 5, -7.9 10e             /float and real vectors
(1.00;2.40;5.00;-7.90e;10.00e)

Cast arguments to ensure vector results.

q)v:1 2.34 -567.1 20e
q)v,(type v)$789                / cast an int to a real
1.00 2.34 -567.1 20.00 789e
q)v,(type v)$1b                 / cast a boolean to a real
1.00 2.34 -567.1 20 1e
q)v,(type v)$0xab
1.00 2.34 -567.1 20.00 171e

,(join) is a multithreaded primitive.

Dictionaries

When both arguments are dictionaries, Join has upsert semantics.

q)(`a`b`c!1 2 3),`c`d!4 5
a| 1
b| 2
c| 4
d| 5

Tables

Tables can be joined row-wise.

q)t:([]a:1 2 3;b:`a`b`c)
q)s:([]a:10 11;b:`d`e)
q)show t,s
a  b
----
1  a
2  b
3  c
10 d
11 e

uj union join
SQL UNION ALL

Tables of the same count can be joined column-wise with ,' (Join Each).

q)r:([]c:10 20 30;d:1.2 3.4 5.6)
q)show t,'r
q)show t,'r
a b c  d
----------
1 a 10 1.2
2 b 20 3.4
3 c 30 5.6

Join for keyed tables is strict; both the key and data columns must match in names and datatypes.

,: Append

x,:y   ,:[x;y]

Where

  • x is a variable containing a list or dictionary
  • y is an atom or a list if x contains a list
  • y is a dictionary if x contains a dictionary

Appends the item(s) of y to the variable x. This is the assign through operator form of ,, but it has major differences.

If x contains a simple list, y must be an atom or simple list of the same type. If x contains a dictionary whose values are a simple list, y must be a dictionary with values of the same type.

q)s:1 2 3
q)s,:4
q)s
1 2 3 4
q)s,:5f
'type
  [0]  s,:5f
        ^
q)s:([a:1;b:2])
q)s,:([a:3;c:4])
q)s
a| 3
b| 2
c| 4
q)s,:([d:5f])
'type
  [0]  s,:([d:5f])
        ^

If x contains a general list, any item(s) can be appended. However, if the rank (defined as the recursive depth of the first element) of x is one higher than that of y, y is implicitly enlisted. This gives a different result from x:x,y.

q)s:(::;3;4)
q)s,:5f
q)s
::
3
4
5f
q)s:enlist 1 2 3    / rank 2
q)s,4 5 6           / rank 1
1 2 3
4
5
6
q)s,:4 5 6
q)s
1 2 3
4 5 6

.Q.dd join symbols
Joins