Re: Problems with Evaluate[Symbol[xxx]]

*To*: mathgroup at smc.vnet.net*Subject*: [mg102785] Re: Problems with Evaluate[Symbol[xxx]]*From*: Szabolcs <szhorvat at gmail.com>*Date*: Thu, 27 Aug 2009 06:34:18 -0400 (EDT)*References*: <h73760$b6u$1@smc.vnet.net>

On Aug 26, 1:44 pm, Keelin <keelinm at gmail.com> wrote: > Hi, > I'm trying to use the Evaluate[Symbol[xxx]] syntax but I think I am > misunderstanding something as it is not behaving as I expect. Below > is an example to illustrate what is unexpected: > The questions are: > 1) Is there a problem with line 604 - it gives errors, but yet the > output in the end is correct Yes. The error will occur the second time that line is evaluated. In short, the first time the exprssion evaluates to abc = {{7,8}}. The second time the expression evaluates to {{7,8}} = {{7,8}}, because abc already has a value. Trying to assign a list of numbers to itself gives an error. You can use the Trace function to see each step of the evaluation of an expression. > 2) Why are the output of abc and the output of Evaluate[Symbol > [myListIdentifier]] not regarded as equal even though they appear > identical (See line 608) You have a typo there. You wrote listIdentifier, not myListIdentifier. > 3) Why can I append correctly using the reference name abc, but not > using the Evaluate[Symbol[myListIdentifier]] name? This is what I > really want to do in the end - append to a list which has a > dynamically assigned variable name. Again, you have the same typo. Here's how to assign to a variable whose name is stores in a string varName: varName = "abc" ToExpression[varName, InputForm, Function[var, var = 123, HoldAll]] As you can see, this is hairy and difficult to handle. In fact it is almost never necessary to use hacks like this in Mathematica. You did not say why you wanted to do this, so I'll just mention one alternative: if you need several variables/symbols, a1, a2, a3, etc., consider using a[1], a[2], a[3] instead. In most contexts, the compound a[1] can be used without any problems in place of the atomic symbol a1. > > Thanks!! > > In[602]:= (*Make an identifier string to hold some data*) > myListIdentifier = "abc" > > Out[602]= "abc" > > In[603]:= (*here's the data*) > myList = {{7, 8}} > > Out[603]= {{7, 8}} > > In[604]:= (*Now put the data into a variable with name abc*) > Evaluate[Symbol[myListIdentifier]] = myList > > During evaluation of In[604]:= Set::setraw: Cannot assign to raw \ > object 7. >> > > During evaluation of In[604]:= Set::setraw: Cannot assign to raw \ > object 8. >> > > Out[604]= {{7, 8}} > > In[605]:= > (*Check abc holds the correct data*) > abc > > Out[605]= {{7, 8}} > > In[606]:= (*Check the data can also be accessed using the \ > myListIdentifier*) > > In[607]:= Evaluate[Symbol[myListIdentifier]] > > Out[607]= {{7, 8}} > > In[608]:= (*But apparently the two items are not equal... why??*) > abc == Evaluate[Symbol[listIdentifier]] > > Out[608]= False > > In[609]:= (*Even worse, I can append using the reference abc, but not > \ > using the myListIdentifier*) > Append[abc, {3}] > > Out[609]= {{7, 8}, {3}} > > In[610]:= Append[Evaluate[Symbol[listIdentifier]], {3}] > > Out[610]= {{3}}