MathGroup Archive 2007

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

Search the Archive

Re: removing those annoying $$$$'s from local variables names?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg79696] Re: removing those annoying $$$$'s from local variables names?
  • From: Jon McLoone <jonm at wolfram.co.uk>
  • Date: Thu, 2 Aug 2007 03:46:40 -0400 (EDT)
  • References: <f8pk78$2i1$1@smc.vnet.net>

On Aug 1, 10:36 am, Nasser Abbasi <n... at 12000.org> wrote:
> Hello;
>
> This is a problem I am sure many have seen. May be there is a simple
> solution for it, but I can't find it.
>
> This problem is when a function returns back a symbol which is local
> to the function, then these synbols will have $nnnn tacked to them,
> and so when the caller say prints the symbol, it will have those $'s
> numbers, which make it annoying to look it and print, say as a table
> or such.
>
> This is an example:
>
> ------------- example 1 -------------
>
> foo[] := Module[{c, k},
>               c = Array[k, 3]
>           ];
>
> c = foo[];
> Print[c];
>
> {k$76[1], k$76[2], k$76[3]}
> --------------------------------------------------
>
> You see, since k is local to foo[], then when I print it, I see those $
> $$ signs.
>
> Only way to remove this $$$'s is to make k global as follows
>
> ------------------------ example 2 -------------------
> Remove[k];
> foo[] := Module[{c},
>               c = Array[k, 3]
>           ];
>
> c = foo[];
> Print[c];
>
> {k[1], k[2], k[3]}
> ----------------------------------------
>
> But making 'k' global is something I do NOT want to do, since now I
> have to worry about 'k' having a value somewhere else in the code, and
> it goes against the whole idea of encapsulation. If I start making all
> my symbols global, then the code becomes hard to manage.
>
> So, what should one do in this case?  How can make all the symbols
> local to a function, but at the same time not see these $$$'s when the
> symbols are used when they are returned from the function back to the
> caller?
>
> thanks,
> Nasser

It depends on whether you want two separate calls to foo to return the
same k or different k? Put it another way should
foo[]-foo[] return
{0,0,0} or a pretty version of {k$1[1]-k$2[1],k$1[2]-k$2[2],k$1[2]-k
$2[2]}

If it is the former user Block instead of Module.

In[1]:= foo[] := Block[{c, k}, c = Array[k, 3]];

In[2]:= foo[]

Out[2]= {k[1], k[2], k[3]}

In[3]:= foo[] - foo[]

Out[3]= {0, 0, 0}

But note that this is local within the Block but becomes global as it
exits. eg
In[4]:= x = 1;
Block[{x},
 {x === 1, x}]

Out[5]= {False, 1}

The Block returns {False,x} but then x picks up the global value
because it is now outside of the Block.

If you want the other result, then you need your k to be distinct
variables like k$1, k$2 but you want a more pleasing name. Well
construct the name yourself. eg k1,k2 etc could be done like this...

In[5]:= foo[] :=
  Block[{c, k = Symbol["k" <> ToString[$ModuleNumber++]]},
   c = Array[k, 3]];
c = foo[];
Print[c];

During evaluation of In[5]:= {k67[1],k67[2],k67[3]}

In[8]:= foo[] - foo[]

Out[8]= {k68[1] - k69[1], k68[2] - k69[2], k68[3] - k69[3]}

This distinction is only necessary because of the symbolic
capabilities of Mathematica. If your output is always numerical there
is no conceptual difference between the application of Block and
Module.

Jon McLoone
http://members.wolfram.com/jonm






  • Prev by Date: Re: removing those annoying $$$$'s from local variables names?
  • Next by Date: Density Plot coloring issue
  • Previous by thread: Re: removing those annoying $$$$'s from local variables names?
  • Next by thread: Re: removing those annoying $$$$'s from local variables names?