[Date Index]
[Thread Index]
[Author Index]
Re: copying a variable
*To*: mathgroup at smc.vnet.net
*Subject*: [mg92336] Re: copying a variable
*From*: Szabolcs Horvát <szhorvat at gmail.com>
*Date*: Sat, 27 Sep 2008 22:23:00 -0400 (EDT)
*Organization*: University of Bergen
*References*: <gbl303$m03$1@smc.vnet.net>
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
>
I don't understand your question fully. What is the purpose of having
two symbols with the same definition? Why couldn't you just do r[1] = 4
instead of r = sub[k]?
Are you trying to transfer the other definitions associated with k to r,
so that if k[2]=Pi was defined then after evaluating r = sub[k], r[2]=Pi
would be automatically defined too?
I suspect that there might be better solutions to your *actual* problem
than copying definitions ... But if you really want to do this, you
could try something like DownValues[r] = DownValues[k] /. k :> r for
copying ... Be warned that this is very bug prone and is very likely to
cause trouble with non-trivial definitions.
Prev by Date:
**Re: Debracketing array symbols**
Next by Date:
**Re: A Package Function Tutorial**
Previous by thread:
**Re: copying a variable**
Next by thread:
**Re: copying a variable**
| |