MathGroup Archive 2005

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

Search the Archive

Re: SymbolName question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61843] Re: [mg61794] SymbolName question
  • From: Igor Antonio <igora at wolf-ram.com>
  • Date: Tue, 1 Nov 2005 00:39:33 -0500 (EST)
  • Organization: Wolfram Research, Inc.
  • References: <200510300443.AAA09790@smc.vnet.net>
  • Reply-to: igora at wolf-ram.com
  • Sender: owner-wri-mathgroup at wolfram.com

Matt wrote:
> Hello,
>    It seems that if I want to print the 'name' of a symbol, that I need
> to save it in a variable before I actually use it.  Is this expected
> behaviour?  It would be imminently more useful if I could give any
> symbol to SymbolName and it would give me a string representation of
> the variable name.
> e.g.
> 
> a1Name = SymbolName[a1];
> a1 = Array[r, {2, 2, 2}];
> Print["The symbol ", a1Name, " has a value of ", a1];
> 
> works, but if I try this:
> 
> a1 = Array[r, {2, 2, 2}];
> Print["The symbol ", SymbolName[a1], " has a value of ", a1];
> 
> it doesn't, and I get the following error:
> SymbolName::sym : Argument {nested list} at position 1 is expected to
> be a symbol.  More...
> 
> The online help states the following:
> "Once you have made an assignment such as x = 2, then whenever x is
> evaluated, it is replaced by 2. Sometimes, however, you may want to
> continue to refer to x itself, without immediately getting the value of
> x.
> 
> You can do this by referring to x by name. The name of the symbol x is
> the string "x", and even though x itself may be replaced by a value,
> the string "x" will always stay the same."
> 
> This seems to indicate that what I want to accomplish should in fact
> work without the need to first save the string representation of a
> symbol name before any value is assigned to it.
> 

Matt,

Not really.  I guess it could have been written more clearly, but it's simply 
saying that you have two things: the name of a symbol and the value of a symbol. 
  The name of *symbol* x is the string "x".  While the value of x can change, 
it's name won't.  It doesn't mention that you can extract the name of x with 
SymbolName.  In fact, the reference guide for SymbolName states that it will 
evaluate it's argument before returning.

Take a look at the example that follows the paragraph you quoted:

t = {SymbolName[x], SymbolName[xp]} // InputForm
x = 2
{x, xp} // InputForm
t // InputForm

That's the same thing as your first example in your email, that is, store the 
name of the variable somewhere.

What you need to use is the function HoldForm.

In[11]:= HoldForm[a1]

Out[11]= a1

In[12]:= Print["The symbol ",HoldForm[a1]," has a value of ",a1];

The symbol "a1" has a value of 
{{{r[1,1,1],r[1,1,2]},{r[1,2,1],r[1,2,2]}},{{r[2,1,1],r[2,1,2]},{r[2,2,1],r[2,2,2]}}}


> Matt



-- 


Igor C. Antonio
Wolfram Research, Inc.
http://www.wolfram.com

To email me personally, remove the dash.


  • Prev by Date: Re: Some Mathematica tips & tricks
  • Next by Date: Re: Re: preserving order of eigenvalues in a matrix diagonalization
  • Previous by thread: Re: Some Mathematica tips & tricks
  • Next by thread: Re: Re: preserving order of eigenvalues in a matrix diagonalization