MathGroup Archive 2003

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

Search the Archive

Re: Symbols and Lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39913] Re: Symbols and Lists
  • From: "Carl K. Woll" <carl at woll2woll.com>
  • Date: Tue, 11 Mar 2003 02:38:29 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

The other responses work fine when the symbol in the list of symbols has no
value. However, if you assign a value to that symbol, and then later on you
want to assign a different value to the same symbol, then you are out of
luck with those approaches.

For example, the first time you run

In[41]:=
Evaluate[ss[[3]]] = dd[[4]]
Out[41]=
7

everything works nicely. But if you decide to assign a different value:

In[43]:=
Evaluate[ss[[3]]] = dd[[5]]
Set::setraw: Cannot assign to raw object 7.
Out[43]=
abc

Now you get an error message, and ss[[3]] still has the value 7. A better
approach would be to get the symbol you want from ss without evaluating it,
and then to go ahead and give it a value. The following approach does this:

In[31]:=
SetAttributes[partset, HoldFirst]
partset[l_, spec_, v_] :=
  Extract[OwnValues[l], Append[{1, 2}, spec], Hold] /. Hold[a_] :> =
Set[a, v]

I'm sure there is a cleaner approach, but the above will work. For example

In[57]:=
ss[[3]]
partset[ss, 3, dd[[5]]]
ss[[3]]
Out[57]=
7
Out[58]=
abc
Out[59]=
abc

and we see that initially ss[[3]] (or c) had the value 7 from what we did
above, but after the partset command ss[[3]] has the value abc.

Carl Woll
Physics Dept
U of Washington

"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
>
>




  • Prev by Date: SetPrecision & Plot bug?
  • Next by Date: Re: Re: How to convert string to variable name
  • Previous by thread: Re: Symbols and Lists
  • Next by thread: RE: Re: Symbols and Lists