Re: about scoping in modules
- To: mathgroup at smc.vnet.net
- Subject: [mg85913] Re: about scoping in modules
- From: Oliver Ruebenkoenig <ruebenko at uni-freiburg.de>
- Date: Wed, 27 Feb 2008 04:32:24 -0500 (EST)
- References: <fpkvqk$hrr$1@smc.vnet.net> <fpmeur$8em$1@smc.vnet.net>
On Tue, 26 Feb 2008, David Bailey wrote: > Oliver Ruebenkoenig wrote: >> On Fri, 22 Feb 2008, David Bailey wrote: >> >>> P_ter wrote: >>>> Hello, >>>> in the next two modules I would like that the inner module uses a variable from the outer module. >>>> mfb[oneObj_] := Module[{ttmp = oneObj},AppendTo[ global2mfb, ttmp]] >>>> mfa[e_] := Module[{global2mfb = e, t = 3},mfb[t] ] >>>> mfa[{2,3}] gives a message that global2mfb is not a variable with a value, so it can not be changed. >>>> It means I do not understand the concept of module in Mathematica. My questions are: >>>> 1. why does it go wrong? >>>> 2. how can I realize what I want? >>>> with friendly greetings, >>>> P_ter >>>> >>> Well, despite its name, global2mfb is local to the Module in which it >>> appears - so using it elsewhere refers to a global variable of the same >>> name with no value - hence the diagnostic. This is the purpose of Module >>> - to stop one use of a variable clashing with another. >>> >>> Take the assignment global2mfb=e outside the list of local >>> initialisations, and I think you will get what you want. >>> >> >> almost, >> >> AppendTo needs a reference not an explicit List >> >> such as >> >> x={1,2} >> AppendTo[x,1] >> >> AppendTo[{1,2},3] can not work since: >> Attributes[ AppendTo] >> >> you could not access the result. >> >> Oliver >> >> Oliver Ruebenkoenig, <ruebenko AT uni-freiburg.de> >> > Sorry, I don't know what you mean. Here is the original code modified as > I suggested: > > In[86]:= mfb[oneObj_]:=Module[{ttmp=oneObj},AppendTo[global2mfb,ttmp]]; > mfa[e_]:=Module[{t=3}, > global2mfb=e; > mfb[t]] > In[88]:= mfa[{2,3}] > Out[88]= {2,3,3} > > AppendTo is operating on a variable (or reference, if you prefer)! > > David Bailey > http://www.dbaileyconsultancy.co.uk > > Sorry, David, you are right. I do not know what I was thinking - probably not at all :-( Oliver Oliver Ruebenkoenig, <ruebenko AT uni-freiburg.de>