Re: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
Re: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
- Subject: Re: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
- From: Ryan Wilcox <email@hidden>
- Date: Thu, 28 Jul 2005 13:55:48 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 7/27/05, at 10:44 PM, Ryan Wilcox said:
[snip sample code]
>
>I get {1, 2}, {{42}, {2}}, and {1}, respectively. So obviously I'm not
>copying a reference to sk1 with my shallow copy.
I was expecting to get {1, 2}, {{42}, {2}, and {42} instead of what I
did get. This (simpler example) gives me the results I expected
initially:
(*<code language="Applescript">*)
set a to {1}
set b to {a}
set c to 42
set item 1 of item 1 of b to c
log a -- is now {42}
(*</code>*)
My first snippet I expected the value pointed to by the reference to get
replaced, but now I think I see what's happening - the reference itself
is getting replaced. The code above correct does what I thought the
other code would do (replace the value pointed to by the reference.)
(As I'm composing this I'm reading NIgel's email, where he says almost
the same thing, but in more words. And with almost the same example.)
_________________________________________________
Back to has' code:
>set item 1 of myList to 0 -- modify content of main list
>log myList --> {0, {2}}
>log myList2 --> {0, {2}}
>log shallowCopy --> {{99}, {2}} -- note, no change
myList2 is just a reference to myList, so it changes as the other
changes.
So then shallowCopy is what its name implies: it is a copy of the
references of myList. Not a reference TO myList, as myList2 is, but a
copy of the _item references_ of myList. Thus, changing the base
variable, a, is reflected in all three lists. myList2 is a reference to
myList, and myList contains a reference to a. shallowCopy has a
completely separate reference to a, thus also reflects the change.
So, to append to the example script posted by has:
(*<code language="Applescript>*)
set item 1 of item 1 of shallowCopy to 57
log myList --> {{57}, {2}}
log myList2 --> {{57}, {2}}
log shallowCopy --> {{57}, {2}}
log deepCopy --> {{1}, {2}} -- note, no change
(*</code>*)
Note: you have to insert this BEFORE the last section, or you won't see
the results I get, because the references to myList get replaced with
(the value 0).
I hope this helps (I know it certainly did me!)
_Ryan Wilcox
================================================================
Wilcox Development Solutions: <http://www.wilcoxd.com>
Toolsmiths for the Internet Age PGP: 0x2F4E9C31
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)
iD8DBQFC6RwebYO0qC9OnDERArs7AJ9zuvj7O0HWORSEal2c7oq1xgKmlACgmoUA
X9qiLUb6FC/2RjzIaHuaKTU=
=oFxs
-----END PGP SIGNATURE-----
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden