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


--- Yuvaraj Athur Raghuvir <[EMAIL PROTECTED]> wrote:

> Pacal, the solution proposed has a problem as it does not scale right.
> Looks like there is an assumption on the shape somewhere.
> 
> Let me call the rajoin you have proposed as rajoin1.
> 
>    rajoin1 =: 4 : '(x ,. ]) (({: |: x) i.~ ({. |: y)) { {: |: y'
> 
> Now, try this:
>    x =. 15 3 $ 12 | 45 ?. 200
>    y =. 10 4 $ 12 | 40 ?. 60
> 
>    x rajoin1 y
> |index error: rajoin1
> |   (x,.])(({:|:x)i.~({.|:y))    {{:|:y
> 
> However,
>    x rajoin y
> +----+--+-------+
> |2 6 |7 |7 0 5  |
> +----+--+-------+
> |2 6 |7 |1 3 3  |
> +----+--+-------+
> |7 6 |5 |11 8 8 |
> +----+--+-------+
> |8 6 |0 |0 9 3  |
> +----+--+-------+
> |4 2 |4 |9 5 8  |
> +----+--+-------+
> |8 10|5 |11 8 8 |
> +----+--+-------+
> |6 11|7 |7 0 5  |
> +----+--+-------+
> |6 11|7 |1 3 3  |
> +----+--+-------+
> |7 10|5 |11 8 8 |
> +----+--+-------+
> |9 2 |4 |9 5 8  |
> +----+--+-------+
> |11 8|6 |11 1 10|
> +----+--+-------+
> |0 5 |10|8 8 1  |
> +----+--+-------+
> |0 5 |10|11 4 3 |
> +----+--+-------+
> |0 5 |10|4 3 7  |
> +----+--+-------+
> 
> ]x
>  2  6  7
>  7  6  5
>  8  6  0
>  4  2  4
>  9  4  9
>  8 10  5
>  6 11  7
>  7 10  5
>  4 11  3
>  2  1  9
>  9  2  4
> 11  8  6
> 11  0  1
>  0  5 10
>  5 10 11
>  ]y
>  6 11 1 10
> 10  8 8  1
>  5 11 8  8
>  2  0 1  5
> 10 11 4  3
>  7  7 0  5
>  0  0 9  3
> 10  4 3  7
>  4  9 5  8
>  7  1 3  3
> 
> 
> On 10/23/06, Pascal Jasmin <[EMAIL PROTECTED]> wrote:
> > this will work if y is sorted (easily fixed if not) and indexed 
> > continuously (left column)
> from 1 to #y
> >
> > (x ,. ]) (1 -~ {: |: x) { {: |: y
> >
> > otherwise, we need to make an indexlookup verb (just i.~) as the left 
> > argument for { .
> >
> > (x ,. ]) (({: |: x) i.~ ({. |: y)) { {: |: y
> > rajoin =: 4 : '(x ,. ]) (({: |: x) i.~ ({. |: y)) { {: |: y'
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to