• 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
Best Practice: Lists: Use COPY instead of SET
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best Practice: Lists: Use COPY instead of SET


  • Subject: Best Practice: Lists: Use COPY instead of SET
  • From: Jim Underwood <email@hidden>
  • Date: Mon, 13 Jun 2016 04:47:58 +0000
  • Thread-topic: Best Practice: Lists: Use COPY instead of SET

Recently, in another forum/list, Chris Stone made an important point about lists.
He made the point that
set listA to listB

just creates a reference to listB.

So, I created a "Best Practice", IMO, and saved it in my Evernote account.
I'd like to share that with everyone here, to possibly help those who are unaware, and to get any feedback for those who know.

Script:



(*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

USING LISTS:  COPY VS SET

-----------------------------------------------------

If you use "set", the new list will just create a reference to the original

Then, if you change the new list, it also changes the old list, and vise-versa


BEST PRACTICE:

  • Use COPY unless you need a reference to original list

    copy origList to newList

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*)


----------------------------------

### EXAMPLE USING SET LIST ###

----------------------------------


set origList to {"one", "two", "three"}


----------------------------

set newList to origList

----------------------------


set item 2 of newList to "changed from newList"

set item 3 of origList to "changed from origList"


-- At this point both origList and newList have been changed,

-- and both are identical.


origList

--> {"one", "changed from newList", "changed from origList"}


newList

--> {"one", "changed from newList", "changed from origList"}


----------------------------------

### EXAMPLE USING COPY LIST ###

----------------------------------


-- If you use "copy", then the new list will be independent

-- from the original list


set origList to {"four", "five", "six"}


----------------------------

copy origList to newList

----------------------------


set item 2 of newList to "changed from newList"

set item 3 of origList to "changed from origList"


-- Both lists have been changed, but have NO effect on one another


origList

--> {"four", "five", "changed from origList"}


newList

--> {"four", "changed from newList", "six"}




Best Regards,

 

Jim Underwood

aka JMichaelTX

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Next by Date: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Previous by thread: Re: How Do I Handle NSNumber Returned in ASObjC?
  • Next by thread: Script Actions in Tk Windows?
  • Index(es):
    • Date
    • Thread