Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A list based table join?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112967] Re: A list based table join?
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Fri, 8 Oct 2010 04:49:42 -0400 (EDT)

x = {{"hello", 27}, {"bye", 30},
   {"hello", 54}, {"seeya", 100}, {"hi", 9}};

y = {"hello", "hi"};

Select[x, MemberQ[y, #[[1]]] &][[All, 2]]

{27,54,9}

Cases[x, t_?(MemberQ[y, #[[1]]] &) :> t[[2]]]

{27,54,9}

x /. {
  {Alternatives @@ y, t_} -> t,
  {_, _Integer} -> Sequence[]}

{27,54,9}

Select[x /. {Alternatives @@ y, t_} :> t,
 IntegerQ]

{27,54,9}

Cases[x /. {Alternatives @@ y, t_} :> t,
 _Integer]

{27,54,9}


Bob Hanlon

---- Leo Ditolaghi <leoditolaghi at gmail.com> wrote: 

=============
Hi all,

(noob Mathematica question:)

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}

I know this should be easy and elegant, but I am going in circles
with list functions.

Thanks in advance,

Leo



  • Prev by Date: Re: A list based table join?
  • Next by Date: Re: Difficulty with Interpolation / FunctionInterpolation
  • Previous by thread: Re: A list based table join?
  • Next by thread: Re: A list based table join?