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: [mg101803] Re: Modify variable names in a loop
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Fri, 17 Jul 2009 05:05:55 -0400 (EDT)
  • References: <h3n515$1qm$1@smc.vnet.net>

On Jul 16, 3:04 pm, Parita <parita.patel at gmail.com> 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

Do[
 Evaluate@Symbol["output" <> ToString[kk]] = {kk, kk + 1, kk + 2},
 {kk, 1, 3}
]

Note that the semicolons were not necessary.

Do consider using

Do[
 output[kk] = {kk, kk + 1, kk + 2},
 {kk, 1, 3}
 ]

instead (with output[1], output[2], etc.)  It is often easier to
handle expressions of this type.


  • Prev by Date: Re: Modify variable names in a loop
  • Next by Date: Re: Questions about assumptions
  • Previous by thread: Re: Modify variable names in a loop
  • Next by thread: Re: Modify variable names in a loop