Re: How to manipulate globals in a function?
- To: mathgroup at smc.vnet.net
- Subject: [mg73728] Re: How to manipulate globals in a function?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 25 Feb 2007 04:46:33 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <erop34$94v$1@smc.vnet.net>
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