MathGroup Archive 2007

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

Search the Archive

Re: How to manipulate globals in a function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73703] Re: How to manipulate globals in a function?
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Sun, 25 Feb 2007 04:33:07 -0500 (EST)
  • 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
> 
> 
Hello,

As written, you end up executing something like:

{1,1,1,1}[[1]]=7

because the argument A gets evaluated - so one way is to prevent that 
evaluation:

SetAttributes[Moo, HoldFirst];
Moo[A_, i_Integer] := (A[[i]] = 7);

Note that the first argument to Moo now matches a symbol, not a list, 
because it is no longer evaluated.

David Bailey
http://www.dbaileyconsultancy.co.uk






  • Prev by Date: Re: How to manipulate globals in a function?
  • Next by Date: Tr[list] vs Plus@@list
  • Previous by thread: Re: How to manipulate globals in a function?
  • Next by thread: Re: How to manipulate globals in a function?