MathGroup Archive 2008

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

Search the Archive

Re: Assignment problem


> I would like to do something like:
> For[ i=1, i<n, i++, ToExpression[ "q" <> ToString[i] ] = 0 ]

This will work, at least one time, after that, you will get error messages:

For[ i=1, i<n, i++, Evaluate[ToExpression[ "q" <> ToString[i] ]] = 0 ]

Note that usually using a Do-Loop is the better choice in Mathematica:

Do[Evaluate[ToExpression[ "q" <> ToString[i] ]] = 0 ,{i,1,n}]

Now if you want to make this work even when there are definitions 
already there for qi then the following should work:

   Append[ToExpression["q" <> ToString[i], InputForm, Hold], 0]
  {i, 1, n}

> I would not like to use the easy way out:
> For[ i=1, i<n, i++, q[i] = 0 ]
> of employing q[i] instead of qi.

Why not? It usually is a much better approach...



  • Prev by Date: Re: Assignment problem
  • Next by Date: ResetMenusPacket in version 6
  • Previous by thread: Re: Assignment problem
  • Next by thread: Re: Assignment problem