Re: Assign value to variable
- To: mathgroup at smc.vnet.net
- Subject: [mg111243] Re: Assign value to variable
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 25 Jul 2010 01:57:00 -0400 (EDT)
On 7/24/10 at 5:07 AM, miguelwon at gmail.com (miguelwon) wrote:
>I'm working with some expressions that are dependent of several
>variables co1, co2, ... , con. I would like to know how can I assign
>values iteratively to some of these variables. I tried:
>For[i=1,i<=100,i++, Symbol["co"<>ToString[i]]=0;
>];
>but it doesn't work. For each iteration it says coi is Protected.
>Can someone help me?
Assuming the variables you want to assign values to do not have
values already assigned to them, then the following will do what
you want.
In[1]:= Set[Evaluate[ToExpression["c" <> ToString[#]]], #] & /@
Range[5];
{c1, c5}
Out[2]= {1,5}
This doesn't work if the variables have values assigned to them
since the Evaluate will cause the variable to be evaluated to
its value resulting in a non-sensical attempt to assign a number
to a number.