For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > February 2007 > How to manipulate globals in a function?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to manipulate globals in a function?
davisjf@gmail.com

2007-02-24, 4:25 am

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


David Bailey

2007-02-25, 4:16 am

davisjf@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





oshaughn

2007-02-25, 4:16 am

This is an order-of-evaluation issue : when mathematica evaluates a
function, the arguments are evaluated *first*.
So for example if I have
f[a_,b_] := a=b
then
a=3
Trace[ f[a,2]]
returns an error, because the first step in evaluation is to replace
'a' by 3, before the function is called.
{{a, 3}, f[3, 2], 3 = 2, {Message[Set::
setraw, 3], {Set::setraw, Cannot assign to raw object `1`.},


However, the mathematica attribute HoldFirst fixes this problem,
preventing the first argument from being evaluated "too soon"
SetAttributes[f, HoldFirst]
Trace[f[a,2]]
--> {f[a, 2], a = 2, 2}

On Feb 24, 1:22 am, davi...@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




Bob Hanlon

2007-02-25, 4:16 am

Use HoldFirst to make the first argument to Moo a Symbol vice a List.

ClearAll[Moo];
SetAttributes[Moo,{HoldFirst}];
Moo[A_Symbol,i_Integer?Positive]:=A[[i]]=7;

A=Table[Random[],{5}];

Moo[A,1];

Verifying that A has been modified:

A

{7,0.359118,0.568554,0.0996048,0.856377}


Bob Hanlon

---- davisjf@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



Jean-Marc Gulliet

2007-02-25, 4:16 am

davisjf@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

Jens-Peer Kuska

2007-02-28, 4:19 am

Hi,

and simsalibim & abrakadabra

SetAttributes[Moo, HoldFirst]

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

will work.

Regards
Jens

davisjf@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
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com