Re: Replacement Rule
- To: mathgroup at smc.vnet.net
- Subject: [mg32072] Re: Replacement Rule
- From: Erk Jensen <Erk.Jensen at cern.ch>
- Date: Sat, 22 Dec 2001 04:22:44 -0500 (EST)
- References: <9vuuha$onq$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Brian Higgins wrote:
>
> I have the following list
>
> l1={{1,2},{3,4},{5,6}};
>
> and then I operate on it with the following pattern/replacement rule
>
> In[3]:= l1 /. {x_, y_} -> x
>
> Out[3]= {1, 3, 5}
>
> This is what I would expect. Now consider the following list
>
> In[4]:= l2 = {{3, 4}, {5, 6}};
>
> Then I use the same pattern/replacement rule
>
> In[5]:=l2 /. {x_, y_} -> x
>
> Out[5]= {3, 4}
>
> I was hoping to get
>
> {3,5}
>
> What am I missing? The FullForm of l2 is basically the same structure
> as l1, as far as I can tell....
>
> Brian
Replace[l2, rule, {1}]
will give the result you expected. More details in the help on "Replace", along
with Chapter A 3.6 of the Book for the levels. Be aware that "/." does a
"ReplaceAll". It recognizes the pattern given in the rule already at the
outermost level of l2, which is of course a list of two elements itself
(ignoring that these elements again are lists). I guess this sort of explains
it.
Ciao
-erk-
--
Dr.-Ing. Erk JENSEN mailto:Erk.Jensen at cern.ch
CERN PS/RF L19510 http://cern.ch/Erk.Jensen
CH-1211 Geneva 23 Tel.: +41 22 76 74298
Switzerland Fax.: +41 22 76 78510