Re: second simple problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg99704] Re: second simple problem*From*: gdelfino <gdelfino at gmail.com>*Date*: Mon, 11 May 2009 06:27:37 -0400 (EDT)*References*: <gu666q$9tf$1@smc.vnet.net>

On May 11, 4:19 am, Francisco Gutierrez <fgutiers2... at yahoo.com> wrote: > Dear sirs: > I have the following list: > ex={1,5,7,4,"M",6,7,8,9,1,"M",3} > I want to replace the M's in the following way: the first M by 5, and the second by2. > Thus I have a replacement list > rL={5,2} > The problem is to get ={1,5,7,4,5,6,7,8,9,1,2,3} > How can I do this in the most general form (for any length of ex and any number of values of "M")? > Thanks > Francisco Francisco, this is one solution: In[1]:= ex = {1, 5, 7, 4, "M", 6, 7, 8, 9, 1, "M", 3} Out[1]= {1, 5, 7, 4, "M", 6, 7, 8, 9, 1, "M", 3} In[2]:= rL = {5, 2} Out[2]= {5, 2} In[3]:= rules = MapIndexed[{"M", First[#1]} -> Flatten[{rL[[#2]], #1}] &, Position[ex, "M"]] Out[3]= {{"M", 5} -> {5, 5}, {"M", 11} -> {2, 11}} In[4]:= Map[First, MapIndexed[Join[{#1}, #2] &, ex] /. rules] Out[4]= {1, 5, 7, 4, 5, 6, 7, 8, 9, 1, 2, 3} Regards, Gustavo Delfino