MathGroup Archive 2009

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

Search the Archive

Re: Modify variable names in a loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101779] Re: [mg101744] Modify variable names in a loop
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Fri, 17 Jul 2009 05:01:27 -0400 (EDT)
  • References: <200907161215.IAA02231@smc.vnet.net>

On Thu, 16 Jul 2009, Parita wrote:

> Hi
>
> I would like to modify the variable name in a loop and assign values
> to the variable.
>
> For[kk = 1, kk <= 3, kk++,
>    "output" <> ToString[Evaluate[kk]] = {kk,kk+1,kk+2};
> ];
>
> I would like the following output
> output1={1,2,3}
> output2={2,3,4}
> output3={3,4,5}
>
> Although, "output" <> ToString[Evaluate[kk]] evaluates as expected, it
> does not let me assign values. Am I missing something here?
>
> Thanks in advance for your help
>
>

Hi,

you have to convert the "output1" String to an expression and since Set 
has attributes HoldFirst you need to enforce evaluation like here:

For[kk = 1, kk <= 3, kk++,
   Evaluate[ToExpression["output" <> ToString[Evaluate[kk]]]] = {kk,
      kk + 1, kk + 2};
   ];

Does this help?

Oliver


  • Prev by Date: How to make variables local to a particular notebook?
  • Next by Date: MIDI controllers
  • Previous by thread: Modify variable names in a loop
  • Next by thread: Re: Modify variable names in a loop