Back to Join-Each (,')

Joins · Join-Each (,') · Practice Question

Attach Equal-Length Columns with Join-Each

Sign in to track your progress

Question

Given the table:

t:([] names:`Tom`Mary`Jack`Sean`Anne`Roy;
      country:`Ireland`England`France`England`Germany`Scotland)

Join the following two columns to table t:

age:10 13 14 18 21 41
employed:101101b

Expected result:

names country  age employed
---------------------------
Tom   Ireland  10  1
Mary  England  13  0
Jack  France   14  1
Sean  England  18  1
Anne  Germany  21  0
Roy   Scotland 41  1

Solution

t:t ,' ([] age:10 13 14 18 21 41; employed:101101b)

Alternative Solution

@[`.;`t;:;t,'([] age:10 13 14 18 21 41; employed:101101b)]

Explanation

,' is join-each (each-both join) — unlike each-left's ,\:, which pairs one fixed value against every row, join-each pairs corresponding rows from two tables of equal length together side by side, combining their columns row by row.

Since t has 6 rows and the new age/employed table also has 6 rows, t,'([] age:...; employed:...) lines row 0 of t up with row 0 of the new table, row 1 with row 1, and so on — attaching age and employed as new columns without changing the row count.

The alternative reaches the same result via amend: @[.;t;:;...] sets the global variable `t (in the root namespace, `.) to the joined table — functionally identical to the plain reassignment, just expressed as an amend rather than a : assignment.