• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: set myList to myList & ...(Digest, Vol 2, Issue 492)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: set myList to myList & ...(Digest, Vol 2, Issue 492)


  • Subject: Re: set myList to myList & ...(Digest, Vol 2, Issue 492)
  • From: "Nigel Garvey" <email@hidden>
  • Date: Thu, 28 Jul 2005 12:52:06 +0100

Ryan Wilcox wrote on Wed, 27 Jul 2005 22:44:12 -0400:

>On 7/26/05, at 5:00 PM, email@hidden said:

It was has, I believe.

>>No it doesn't. It performs a deep copy of the list object, so any
>>mutable items within that list will also be duplicated. To shallow
>>copy a list, you have to use 'items of <list>'.
>
>What is involved in the shallow copy? I tried the following:
>
>(*<code language="Applescript">*)
>set sk1 to 1
>set sk2 to 2
>set listItem1 to {sk1}
>set listItem2 to {sk2}
>
>set myList to {listItem1, listItem2}

This sets myList to {{1}, {2}}, a list containing two lists. Those lists
_are_ the lists listItem1 and listItem2.

>set mycopy to items of myList

This creates a new list of the 'items' in myList and sets mycopy to the
result. mycopy and myList are different lists in themselves, but contain
exactly the same items: the lists listItem1 and listItem2. Not
duplicates. That's what's meant by a shallow copy. With a deep copy, as
performed by the 'copy' command, the items of mycopy would be duplicates
of listItem1 and listItem2. If listItem1 and listItem2 themselves
contained lists, 'copy' would duplicate those too - and so on.

>set item 1 of mycopy to {42}
>
>log myList
>log mycopy
>log listItem1
>(*</code>*)
>
>I get {1, 2}, {{42}, {2}}, and {1}, respectively.

Actually: {{1}, {2}}, {{42}, {2}}, and {1}

> So obviously I'm not
>copying a reference to sk1 with my shallow copy.

Setting item 1 of mycopy to {42} simply puts a different list into the
item 1 slot of mycopy. It's a direct alteration to mycopy, but not to the
{1} that was there before.

If you make an alteration to the {1} instead of replacing it:

  set item 1 of item 1 of mycopy to 42

... then you'll see the shallowness effect:

  myList --> {{42}, {2}}
  mycopy --> {{42}, {2}}
  listItem1 --> {42}


The 'copy' command duplicates structures rather than simply the items. If
a single item is used twice in the original list, it won't simply be
duplicated twice. It'll be duplicated once and _used_ twice in the
duplicate list:

  set listItem1 to {1}
  set myList to {listItem1, listItem1}
  --> listItem1 used twice in myList

  copy myList to mycopy
  set item 1 of item 1 of mycopy to 42

  myList --> {{1}, {1}} -- as you'd expect

  mycopy --> {{42}, {42}} -- not {{42}, {1}}


>Looking forward to hearing the answers (hah! and you thought this thread
>was dead :-p )

Nah. It just keeps sprouting sub-threads with the subject "Re:
Applescript-users Digest..."

NG

 _______________________________________________
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

  • Prev by Date: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
  • Next by Date: Copy file across volumes
  • Previous by thread: Re: shallow vs deep copying [was Re: Applescript-users Digest, Vol 2, Issue 492]
  • Next by thread: Copy file across volumes
  • Index(es):
    • Date
    • Thread