Re: A list based table join?

*To*: mathgroup at smc.vnet.net*Subject*: [mg112972] Re: A list based table join?*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Fri, 8 Oct 2010 04:50:38 -0400 (EDT)

On 10/7/10 at 3:39 AM, leoditolaghi at gmail.com (Leo Ditolaghi) wrote: >If I have two lists, how do I determine the indices at which the 1st >list elements are matched by the elements of the second, and then >use these matching indices to extract corresponding columns in the >1st list (sort of a simulated database join)? I.e., >x = {{"hello", 27}, >{"bye", 30}, >{"hello", 54}, >{"seeya", 100}, >{"hi", 9}} >y = {"hello", "hi"} >where x is the source table, and y is the select criteria (so to >speak.) >I wish to produce z, where y defines which elements of x to extract: >z = {27,54,9} Here are a few ways to get the result you want: In[3]:= x = {{"hello", 27}, {"bye", 30}, {"hello", 54}, {"seeya", 100}, {"hi", 9}}; y = {"hello", "hi"}; In[5]:= Cases[x, {_?(MemberQ[y, #] &), _}][[All, 2]] Out[5]= {27,54,9} In[6]:= Select[x, MemberQ[y, First[#]] &][[All, 2]] Out[6]= {27,54,9} In[7]:= Cases[x, {Alternatives @@ y, _}][[All, 2]] Out[7]= {27,54,9}