Re: getting the name of a symbol
- To: mathgroup at smc.vnet.net
- Subject: [mg15476] Re: getting the name of a symbol
- From: dreissBLOOP at bloop.earthlink.net (David Reiss)
- Date: Mon, 18 Jan 1999 04:22:16 -0500
- Organization: EarthLink Network, Inc.
- References: <77jjc1$2lf@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
In article <77jjc1$2lf at smc.vnet.net>, Andrew Watson <abwatson at mail.arc.nasa.gov> wrote: > Dear Matherati, > > I have a problem for which there must be a simple solution. > > Within a function > > f[a_] := blah blah blah > > I want to make use of the name of the symbol supplied as input to the > function. > > For example, if called as > > f[qxyz] > > I want to get the string "qxyz". I have looked at HoldForm and > SymbolName but nothing yet discovered seems to do the trick. > > Regards, > > > Andrew B. Watson > MS 262-2 > NASA Ames Research Center > Moffett Field, CA 94035-1000 > (650) 604-5419 (650) 604-0255 fax > abwatson at mail.arc.nasa.gov http://vision.arc.nasa.gov/ I am not quite sure what you are asking here. Here is an example where a symbol that is not a string is input and a new string based on the symbol's name is created as an output (using SymbolName as you alluded to): In[1]:= Clear[f1]; f1[x_]/;(!StringQ[x]):="Big" <> SymbolName[x] In[2]:= f1[z] Out[2]= "Bigz" In[3]:= StringQ[%] Out[3]= True Here is a case where a new symbol is created from the name of the original symbol and then turned into an expression: In[4]:= Clear[f2]; f2[x_]/;(!StringQ[x]):=ToExpression["Big" <> SymbolName[x] ] In[5]:= f2[z] Out[5]= Bigz In[6]:= StringQ[%] Out[6]= False Now give Bigz a value In[7]:= Bigz=7 Out[7]= 7 In[8]:= f2[z] Out[8]= 7 Now really try to break things by giving z a value: In[9]:= z=9 Out[9]= 9 But now this doesn't work: In[10]:= f2[z] Out[10]= $Failed However, this seems to do the job: In[11]:= ClearAll[f3]; Attributes[f3]={HoldAll}; f3[x_]/;(!StringQ[x]):=ToExpression["Big" <> SymbolName[Unevaluated[x]] ] In[12]:= f3[z] Out[12]= 7 Use Trace to see the sequence of evaluatiosn taking place here: In[13]:= Trace[f3[z]] Out[13]= {f3[z],{{{z,9},StringQ[9],False},\[InvisibleSpace]!False,True}, ToExpression["Big"<>SymbolName[Unevaluated[z]]],{{SymbolName[z],"z"}, "Big"<>"z","Bigz"},ToExpression["Bigz"],7} But perhaps you are asking a different question? Cheers, David -- )----------------------------------------( ) Scientific Arts: ( ) Creative Services and Consultation ( ) for the Applied and Pure Sciences ( ) ( ) David Reiss ( ) Email: dreiss at !SPAMscientificarts.com ( )----------------------------------------( Remove the !SPAM to send email