MathGroup Archive 2007

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

Search the Archive

Re: How to manipulate globals in a function?


davisjf at gmail.com wrote:
> I am trying to write code to manipulate a global array in a function.
> How do something like this:
> 
> Moo [A_List, i_Integer] :=
> 
>       A[[i]] = 7;
> 
> Moo[A, 1]
> 
> This fails.  But, if I do this without a funciton it works.
> 
> i=1
> A[[i]] = 7
> 
> JD
> 
> 
Set the attribute HoldFirst.

In[1]:=
moo[a_, n_] := a[[n]] = 7
SetAttributes[moo, HoldFirst]
array = Table[Random[], {10}]
moo[array, 2]
array

Out[3]=
{0.291238, 0.68772, 0.596728, 0.994694, 0.556517,

   0.422771, 0.854193, 0.147855, 0.799394, 0.121512}

Out[4]=
7

Out[5]=
{0.291238, 7, 0.596728, 0.994694, 0.556517, 0.422771,

   0.854193, 0.147855, 0.799394, 0.121512}

Regards,
Jean-Marc


  • Prev by Date: Re: simple question
  • Next by Date: Re: split
  • Previous by thread: Re: How to manipulate globals in a function?
  • Next by thread: Re: How to manipulate globals in a function?