MathGroup Archive 2008

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

Search the Archive

Re: Assignment problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86296] Re: Assignment problem
  • From: Albert Retey <awnl at arcor.net>
  • Date: Fri, 7 Mar 2008 02:33:55 -0500 (EST)
  • References: <fqo8o1$suo$1@smc.vnet.net>

Hi,

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

Do[
  Apply[
   Set,
   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...

hth,

albert


  • 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