Re: copying a variable

*To*: mathgroup at smc.vnet.net*Subject*: [mg92316] Re: copying a variable*From*: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>*Date*: Sat, 27 Sep 2008 22:19:20 -0400 (EDT)*References*: <gbl303$m03$1@smc.vnet.net>

Hi, if you call sub[] with sub[k] k must be affected because sub[k] expand to With[{z=k},k[1]=4; k] and when you return k from you sub[k] call r is set to k and r[1] -> k[1] so that after r=sub[k] r[1] must return the same as k[1] because r is set to k. You can't have a copy of x because x is x, in the best case you can copy the value of x but never x itself. Regards Jens ramiro wrote: > Hello, > This must be very simple. Consider the following code: > > k[1] = 2; > sub[x_] := With[{z = x}, z[1] = 4; z] > r = sub[k]; > r[1] > k[1] > > The output is > 4 > 4 > > I would like to have the assignment inside the function (on z) don't > affect the parameter (x). I would like to copy x so that whatever > assignment I do on z doesn't affect it, in other words, I would like to > return _a copy_ of x. So that the output should be: > > 4 > 2 > > Thanks in advance, > Ramiro >