Oleg, thanks for the solution.
There are some points I donot understand in your solution. Could you
explain them to me?
1) I am not able to understand how the jndx works. Once the
position-match matrix is determined using =/, how are you compressing
it into positions of the tables using the various operators? I tried,
but got confused :(
2) The modeling of join as a adverb puzzles me as well. In general, is
it true that if the verb needs parameters, the implication is to use
an adverb?
Thanks,
Yuva
On 10/23/06, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
To define a relational join, there need to be
an agreement on what columns are matched,
and what are selected.
Suppose, A and B are lists of tupples with attributes
in columns.
A (ia;ib;sa;sb) join B
will return list of tuples, where column ia of A
matches ib of B, with attributes sa from A and sb from B.
jndx=: (0 , [EMAIL PROTECTED]) #: [: I.@, =/ NB. join indices
join=: 1 : 0 NB. A (ia;ib;sa;sb) join B
:
'ia ib sa sb'=. m
'ja jb'=. |:(ia {"1 x) jndx ib {"1 y
(sa {"1 ja { x) ,. sb {"1 jb { y
)
Examples
A=. 1 101,2 102,3 103,:5 104
B=. 1 201,3 202,1 203,2 204,4 205,:3 206
A;B
+-----+-----+
|1 101|1 201|
|2 102|3 202|
|3 103|1 203|
|5 104|2 204|
| |4 205|
| |3 206|
+-----+-----+
A 0 0 1 1 join B
101 201
101 203
102 204
103 202
103 206
X =. 15 3 $ 12 | 45 ?. 200
Y =. 10 4 $ 12 | 40 ?. 60
X;Y
+--------+----------+
| 2 6 7| 6 11 1 10|
| 7 6 5|10 8 8 1|
| 8 6 0| 5 11 8 8|
| 4 2 4| 2 0 1 5|
| 9 4 9|10 11 4 3|
| 8 10 5| 7 7 0 5|
| 6 11 7| 0 0 9 3|
| 7 10 5|10 4 3 7|
| 4 11 3| 4 9 5 8|
| 2 1 9| 7 1 3 3|
| 9 2 4| |
|11 8 6| |
|11 0 1| |
| 0 5 10| |
| 5 10 11| |
+--------+----------+
X (0;0;0 1 2;0 1 2 3) join Y
2 6 7 2 0 1 5
7 6 5 7 7 0 5
7 6 5 7 1 3 3
4 2 4 4 9 5 8
6 11 7 6 11 1 10
7 10 5 7 7 0 5
7 10 5 7 1 3 3
4 11 3 4 9 5 8
2 1 9 2 0 1 5
0 5 10 0 0 9 3
5 10 11 5 11 8 8
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm