Re: Re: Extracting the name of a variable
- To: mathgroup at smc.vnet.net
- Subject: [mg26960] Re: [mg26943] Re: Extracting the name of a variable
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Tue, 30 Jan 2001 03:38:14 -0500 (EST)
- References: <94tl4s$lck@smc.vnet.net> <200101280100.UAA24760@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Mike, Depending on how your list1 was defined, there is a way to extract the symbol names that make up the list. First, a definition for extractNamesFromList: In[27]:= SetAttributes[extractNamesFromList,HoldAll] SetAttributes[SymbolName,HoldAll] extractNamesFromList[a_]:=SymbolName/@Unevaluated[a] For your example, we had In[36]:= q=4; z=5; list1:={q,z,w} Note that the definition of list1 uses a SetDelayed(:=) instead of Set. This is important, as the use of Set would mean that Mathematica would store the information {4,5,w} for list1, and not {q,z,w}. Now, the question is, how does one get at the information that Mathematica knows about list1? The answer is given by the functions DownValues, UpValues, and OwnValues (there are others). So, in this case we want to use OwnValues: In[39]:= OwnValues[list1] Out[39]= {HoldPattern[list1] :> {q, z, w}} So, OwnValues of list1 contains the information you want. All we need to do is extract it using the function Extract. Before I do that, note that extractNamesFromList does what you want when given {q,z,w}. In[46]:= extractNamesFromList[{q,z,w}]//FullForm Out[46]//FullForm= List["q", "z", "w"] So, to extract the symbol names from list1, we can do In[47]:= Extract[OwnValues[list1],{1,2},extractNamesFromList]//FullForm Out[47]//FullForm= List["q", "z", "w"] Extract takes the part of OwnValues[list1] that contains your list of variables, and wraps that list with extractNamesFromList before evaluation takes place. So, it is possible to solve your problem. Carl Woll Physics Dept U of Washington ----- Original Message ----- From: "Mike Yukish" <may106 at psu.edu> To: mathgroup at smc.vnet.net Subject: [mg26960] [mg26943] Re: Extracting the name of a variable > "Ersek, Ted R" wrote: > > > Mike Yukish asked how one can take a variable which has a value assigned and > > convert it to a string without letting it evaluate. > > > > I provide the solution in the section for (ToString) on my web site. The > > URL is > > http://www.verbeia.com/mathematica/tips/Tricks.html > > Many thanks to all who replied. I posed my problem as a subpart of a more > complex problem, which still defeats me. I had hoped to Map[ ] the function > extractName over a list, and extract the names of all the variables in the list. > Ted has identified its difficulties, which I offer up here. > > Problem: Create a function that takes a list of variables, and extracts the > symbol name of each. This is an added level over the initial problem. > > q=4 > z=5 > > list1={q,z,w} > > extractNamesFromList[list1] > > out[]:= > > {"q","z","w"} > > Ted Ersek and Bob Hanlon have both shown me how to create a function that will > work with > > extractNamesFromList[{q,z,w}] > > out[]:= > {"q","z","w"} > > > but extractNamesFromList[list1] > out[]:= > "list1" > > So passing a predeclared list is a different story, and Ted pointed that out. I > think I can live with the restriction, though. > > > >
- References:
- Re: Extracting the name of a variable
- From: Mike Yukish <may106@psu.edu>
- Re: Extracting the name of a variable