Re: Assigning Values to Many Symbols at Once
- To: mathgroup at smc.vnet.net
- Subject: [mg101823] Re: Assigning Values to Many Symbols at Once
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 18 Jul 2009 04:51:12 -0400 (EDT)
On 7/16/09 at 8:22 AM, gregory.lypny at videotron.ca (Gregory Lypny)
wrote:
>Leonid and Zach, both of you suggested direct list-to-list
>assignments, which is what I thought should work but does not with
>you create the list of new symbols using Table or mapping. It does
>work if you create the symbol list manually, For example, typing
>{x1, x2} = {9, 11}
>works as expected, creating two new symbols, x1 with a value of 9
>and x2 with a value of 10. However,
>Table[Symbol["x" <> ToString[i]], {i, 5}] = {1,2,3,4,5}
>and
>Table[Symbol["x"<>ToString[i]],{i,5}]=Table[i,{i,5}]
>result in the error message
>Set::write: Tag Table in Table[Symbol[xabc<>ToString[i]],{i,5}] is
>Protected.
This works with no error message
In[1]:= Evaluate[ToExpression[Table["x" <> ToString[n], {n,
5}]]] =
Range[5]
Out[1]= {1,2,3,4,5}
In[2]:= x1
Out[2]= 1
or you can do
In[3]:= Evaluate[Table[Symbol["y"<>ToString[n]], {n, 5}]] = {1,2,3,4,5}
Out[3]= {1,2,3,4,5}
In[4]:= y1
Out[4]= 1
and get no error message.
Note, both of these examples assume the variables do not
currently exist and have assigned values.