Re: Previous variable affects "/."
- To: mathgroup@smc.vnet.net
- Subject: [mg11311] Re: Previous variable affects "/."
- From: dreissNOSPAM@nospam.earthlink.net (David Reiss)
- Date: Wed, 4 Mar 1998 01:40:14 -0500
- Organization: EarthLink Network, Inc.
- References: <6dg8bd$2tr@smc.vnet.net>
You need to do a delayed replacement using :> (analogous to a delayed assignment) as in the following: In[1]:= a = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; In[2]:=a /. {x_,y_} -> {x} Out[2]= {{1},{3},{5},{7}} In[3]:= x = 7; In[4]:= a /. {x_, y_} -> {x} Out[4]= {{7},{7},{7},{7}} In[5]:= a /. {x_, y_} :> {x} Out[5]= {{1},{3},{5},{7}} Cheers, David In article <6dg8bd$2tr@smc.vnet.net>, Vilis Nams <vnams@nsac.ns.ca> wrote: > I am using "/." to change expressions, as follows: > > In[1]:= a = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; In[2]:= a /. {x_, > y_} -> {x} > Out[2]:= {{1}, {3}, {5}, {7}} > The results are expected. > > But, if I now define x to be something already, e.g. In[3]:= x = 7; > > Then, when I do the Replace, Mathematica uses my previously-defined x, as follows= > : > > In[4]:= a /. {x_, y_} -> {x} > Out[4]:= {{7}, {7}, {7}, {7}} > > I tried to even wrap it inside of a function, to have x as a local > variab= le, as > in: > In[5]:= f[v_] := Module[{x, y}, v /. {x_, y_} -> {x}]; > > But, it still used the x defined previously: In[6]:= f[a] > Out[6]:= {{7}, {7}, {7}, {7}} > > This is no problem when I'm just creating and using variables in one > se= ssion, > because I can remember which variables I created. But, I have such > Replac= e > statements sprinkled inside of functions that I defined. I cannot > remembe= r all > the symbols I used in those definitions. Even worse, I may not get an > err= or > message - I just may get the wrong result without knowing it. > > How can I use the Replace and x_, and make sure that previously-defined > variables don't influence the results? > > > -------------------- > Vilis O. Nams > Dept of Biology, NSAC > Box 550, Truro, NS, Canada > vnams @ nsac.ns.ca > ------------------- -- David Reiss dreissNOSPAM@nospam.earthlink.net http://home.earthlink.net/~dreiss To send personal email, remove the words "nospam" and "NOSPAM" from the email address