Re: How to do a SQL type operation???
- To: mathgroup at smc.vnet.net
- Subject: [mg64392] Re: How to do a SQL type operation???
- From: "Ray Koopman" <koopman at sfu.ca>
- Date: Wed, 15 Feb 2006 03:32:06 -0500 (EST)
- References: <dspfnb$cf2$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Richard Palmer wrote:
> Assume Table1={{a,Xa},{b,Xb},...,{n,Xn},...} where the key a can occur many
> times and Xa is different each occurrence.
>
> We also have table2={{a,Ya},{b,Yb},...,{n,Yn}}. Here a,b,...,n occur exactly
> once.
>
> How do I create table3={{a,Xa,Ya},{b,Xb,Yb},...,{n,Xn,Yn},...} by joining
> the tables on the first column?
>
> From the problem, if a (or b or c or ... n} is in table1, it is also in
> table1 and visa versa.
>
> The a,b,...,n,... Are small positive integers.
>
> Thanks
Ah, the virtues of testing! In this case it showed
the need to parenthesize the function in Scan.
In[1]:= table1 = {{a,Xa},{b,Xb},{c,Xc},{d,Xd},{a,Xaa},{c,Xcc}}
table2 = {{a,Ya},{b,Yb},{c,Yc},{d,Yd}}
Module[{Y},Scan[(Y@#[[1]] = #[[2]])&, table2];
Append[#, Y@#[[1]] ]& /@ table1]
Out[1]= {{a,Xa},{b,Xb},{c,Xc},{d,Xd},{a,Xaa},{c,Xcc}}
Out[2]= {{a,Ya},{b,Yb},{c,Yc},{d,Yd}}
Out[3]= {{a,Xa,Ya},{b,Xb,Yb},{c,Xc,Yc},{d,Xd,Yd},
{a,Xaa,Ya},{c,Xcc,Yc}}