MathGroup Archive 2009

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

Search the Archive

Re: Assigning Values to Many Symbols at Once

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101736] Re: [mg101699] Assigning Values to Many Symbols at Once
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 15 Jul 2009 07:12:49 -0400 (EDT)
  • References: <200907141200.IAA05959@smc.vnet.net>

Hi Gregory,

If your symbols don't have values, you can simply use Set:

 Table[Symbol["x" <> ToString[i]], {i, 5}] = {1,2,3,4,5}

for instance. If they do have values, this is a bit more work. The following
will do what you presumably want, in both cases:

In[1] =

ClearAll[assignToSymbols];
SetAttributes[assignToSymbols, HoldFirst];
assignToSymbols[base_String, {startIndex_Integer, endIndex_Integer},
    values_List] /; endIndex - startIndex + 1 == Length[values] :=
  assignToSymbols @@
   Append[Unevaluated /@ Thread[
      Table[
       ToExpression[base <> ToString[i], InputForm, Hold],
       {i, startIndex, endIndex}], Hold], values];

assignToSymbols[symbs : {___Symbol}, values_List] /;
   Length[Hold @@ symbs] == Length[values] :=
  Unevaluated[symbs] = values;


In[2]=
Clear[x1, x2, x3, x4, x5];
assignToSymbols["x", {1, 5}, Range[5]];
{x1, x2, x3, x4, x5}


Out[2] = {1, 2, 3, 4, 5}


In[3] = assignToSymbols["x", {1, 5}, Range[5, 9]]


Out[3] = {5, 6, 7, 8, 9}


Regards,
Leonid



On Tue, Jul 14, 2009 at 5:00 AM, Gregory Lypny
<gregory.lypny at videotron.ca>wrote:

> Hello everyone,
>
> I can create a bunch of symbols on the fly using the Symbol command,
> as in
>
>        Table[Symbol["x" <> ToString[i]], {i, 5}]
>
> to get
>
>        {x1,x2,x3,x4,x5}
>
> But how can I assign values to these all at once?
>
> Regards,
>
>        Gregory
>
>


  • Prev by Date: Re: Assigning Values to Many Symbols at Once
  • Next by Date: Re: Refine, assumptions, domains
  • Previous by thread: Re: Assigning Values to Many Symbols at Once
  • Next by thread: Re: Assigning Values to Many Symbols at Once