Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Programatically creating delayed

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110241] Re: Programatically creating delayed
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Thu, 10 Jun 2010 08:07:09 -0400 (EDT)
  • References: <201006081106.HAA18787@smc.vnet.net>

Hi Stuart,

for completeness, let me mention that my function will fail in case when
some of the <aelement[i]> has some value at the moment when we create our
delayed assignment. Observe:

In[1]:=
Table[aelement[i] = i, {i, 3}]

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

In[2]:=
Scan[assignElements[#, "element", 3] &, {"a", "b"}]

In[3]:= ?a

Global`a

a:={1,2,3}

This is probably not what you would like. Here is a somewhat more
sophisticated version that is free from this flaw:

Clear[assignElementsFull];
assignElementsFull[name_String, elemName_String, length_Integer] :=
  With[{heldName = ToExpression[name, InputForm, Hold]},
   Clear @@ heldName;
   With[{heldElementBaseName =
      ToExpression[name <> elemName, InputForm, Hold]},
    Block @@ Append[Map[List, heldElementBaseName],
      Unevaluated[
       Evaluate[First@heldName] :=
        Evaluate[
         Table[First[heldElementBaseName][i], {i, length}]]]]]];

For example:

In[6]:=
Clear[a, aelement];
Table[aelement[i] = i, {i, 3}];
assignElementsFull["a", "element", 3]

In[9]:= ?a

Global`a

a:={aelement[1],aelement[2],aelement[3]}

It will work even in this case:

In[10]:=
aelement = 1;

In[11]:= assignElementsFull["a", "element", 3]

In[11]:= ?a

Global`a

a:={aelement[1],aelement[2],aelement[3]}


Regards,
Leonid





On Wed, Jun 9, 2010 at 4:20 AM, Stuart Nettleton <
Stuart.Nettleton at uts.edu.au> wrote:

> Hi Leonid,
> What a wonderful little function!
> Just the thing.
> Many thanks indeed,
> Stuart
>
> --
> UTS CRICOS Provider Code:  00099F
> DISCLAIMER: This email message and any accompanying attachments may contain
> confidential information.  If you are not the intended recipient, do not
> read, use, disseminate, distribute or copy this message or attachments.  If
> you have received this message in error, please notify the sender
> immediately and delete this message. Any views expressed in this message
> are those of the individual sender, except where the sender expressly, and
> with authority, states them to be the views the University of Technology,
> Sydney. Before opening any attachments, please check them for viruses and
> defects.
>
>


  • Prev by Date: Re: difficulty using FindRoot
  • Next by Date: Re: Deleting Duplicates
  • Previous by thread: Re: Programatically creating delayed
  • Next by thread: Re: Programatically creating delayed