Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1998

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

Search the Archive

Re: Disappearing variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13794] Re: Disappearing variables
  • From: Tobias Oed <tobias at physics.odu.edu>
  • Date: Fri, 28 Aug 1998 04:18:19 -0400
  • Organization: Old Dominion University
  • References: <6rr9u0$15b@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

nobody at News.Dal.Ca wrote:
> 
> I want to to define a variable inside a function, as follows: f:=x=123;
> Whenever I run f, x should be given the value of 123, and if x doesn't
> exist, it should be created. However:
> 
> In[1]:= f:=x=123;
> In[2]:=f
> Out[2]:=123
> In[3]:=x
> Out[3]:=123        ** x exists
> In[4]:=Remove[x];
> In[5]:=f
> Out[5]:=123  ** f seems to work
> In[6]:=x
> Out[6]:=x     ** x doesn't exist anymore, even though f worked
> 
> It seems like x is created when I initially define f, whereas since f is
> SetDelayed,  x should be created when I run f.
> Does anyone know why x is not created when I run f?
> 
> Vilis Nams

You need to look at what Remove x is doing:

In[1]:= f:=x=123

In[2]:= ??f
Global`f

f := x = 123

In[3]:= Remove[x]

In[4]:= ??f
Global`f

f := Removed["x"] = 123

That's where your problem comes from. A way arround this is

In[1]:= f:=Evaluate[Symbol["x"]]=123

In[2]:= f

Out[2]= 123

In[3]:= x

Out[3]= 123

In[4]:= Remove[x]

In[5]:= f

Out[5]= 123

In[6]:= x

Out[6]= 123

In[7]:= ??f
Global`f

f := Evaluate[Symbol["x"]] = 123

That's the rule mathematica remembers and is not affected by Remove[x]
because it does not depend on x. The information  you can get using ??
is really practical because it tells  you the rules which are
associated with symbols without  evaluating them, allowing you to see
what is really going on.  By the way it's pretty instructive to
inspecct your symbols and  Blank this way when using Verbatim
definitions.

Enjoy, Tobias


  • Prev by Date: StyleSheets and Palettes in your application
  • Next by Date: Re: questions about make boxes
  • Previous by thread: Re: Disappearing variables
  • Next by thread: Re: Disappearing variables