RE: Re: Symbols and Lists

*To*: mathgroup at smc.vnet.net*Subject*: [mg39902] RE: [mg39858] Re: Symbols and Lists*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>*Date*: Tue, 11 Mar 2003 02:36:17 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

>-----Original Message----- >From: atelesforos at hotmail.com [mailto:atelesforos at hotmail.com] To: mathgroup at smc.vnet.net >Sent: Sunday, March 09, 2003 11:27 AM >To: mathgroup at smc.vnet.net >Subject: [mg39902] [mg39858] Re: Symbols and Lists > > >To assign the j-th element of dd to the i-th element of ss, use >Evaluate[ ss[[i]]] ] = dd[[j]] > >The problem is that once you do this, ss will still change, since the >new definition for eg. c will be used. > >If you want to associate symbols with values without affecting the >symbols (to create a hashtable in other words) do this: >ssHash[symb_Symbol]^=value_ >For instance ssHash[c] ^= dd[[4]]. >Now you can retrieve the 'value' of c with ssHash[c] and still get >back ss. > >Another way to do this is with rules: >ssRules={a->dd[[1]], b->d[[3]], c->d[[4]], ...} >c/.ssRules will return the 'value' of c >and First/@ssRules will give back ss. > >Hope that helped, >Orestis > >Goyder Dr HGD <H.Goyder at rmcs.cranfield.ac.uk> wrote in message >news:<b4c8e4$n7k$1 at smc.vnet.net>... >> Dear MathGroup, >> >> I am doing something much more complicated but this trivial example >> expresses my difficulty. >> >> If I have a list of symbols and a list of data such as >> >> ss = {a, b, c, d, e} >> >> dd = {1, 5, {3, 4}, 7, abc, {0.1, 0.001}, 3.1415} >> >> how do I take a symbol from the list ss and associated it >with data from the >> list dd? >> >> A wrong attempt is to try >> >> ss[[3]] = dd[[4]] >> >> This replaces element 3 in ss with element 4 from dd. >> >> What I need is an assignment so that, in this case, the symbol c is >> associated with the data. Equivalent to writing >> >> c = dd[[4]] >> >> so that c could then be used in subsequent expressions and >the list ss is >> unchanged. >> >> Note that this is a trivial example for pedagogic purposes - >I can't do the >> simple assignment in my actual case. >> >> Thanks >> >> Hugh Goyder > Hugh, Orestis has shown you two ways of how to associate values (of a list given) with symbols (of another list given), including the possibility to reassign this association. Both methods however have repercussions on your original coding. Here I show a way how to do those things and keep (the rest of) the coding you have. In[2]:= ss = Unevaluated /@ {a, b, c, d, e}; Keep a repository of _unevaluated_ symbols. In[3]:= dd = {1, 5, {3, 4}, 7, abc, {0.1, 0.001}, 3.1415}; A repository of values Assign a value to a symbol: In[4]:= With[{symbol = ss[[3]]}, symbol = dd[[4]]] Out[4]= 7 In[5]:= c Out[5]= 7 Reassign a different value to that symbol: In[6]:= With[{symbol = ss[[3]]}, symbol = dd[[5]]] Out[6]= abc In[7]:= c Out[7]= abc The construction With does two things: (1) it gets the indicated unevaluated symbol from the list, and moves that symbol to the lhs of Set (including its wrapper Unevaluated), (2) there the wrapper Unevaluated is being stripped off (but the symbol will not be evaluated further, due to HoldFirst Attribute of Set). Such repeated assignment of symbols from a list become possible. You may Clear (or equally Unset) the symbol: In[11]:= With[{symbol = ss[[3]]}, Clear[symbol] ] In[12]:= c Out[12]= c -- Hartmut Wolf