Re: set myList to myList & ... and unexpected results
Re: set myList to myList & ... and unexpected results
- Subject: Re: set myList to myList & ... and unexpected results
- From: Barry Wainwright <email@hidden>
- Date: Sun, 24 Jul 2005 16:09:26 +0100
- Thread-topic: set myList to myList & ... and unexpected results
On 24/7/05 07:35, "Ryan Wilcox" <email@hidden> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi all,
>
> Perhaps someone here can help me with a puzzle. Run the following code:
>
> - --<code language="Applescript>
>
> set myList to {}
> set myItem to {1}
>
> set myList to myList & myItem
This stores a reference to myitem in the last position of mylist (which,
since the list was empty, is also the first item). This entity and myitem
_are the same thing_
> set item 1 of myList to 123
If you check 'myitem' at this point you will find it contains '123'
>
> set myList to myList & myItem
And so, you get {123, 123}
>
> - --</code>
>
> The output of this code is {123, 123}, and NOT {123, 1} like I would
> expect.
Ah, but I expected this.
This is fundamental applescript, and shows the difference between 'set' and
'copy' - go and read Apple's Applescript Reference Manual for all the
details.
To do what you want, use 'copy', which copies the _value_ of a variable, not
a reference to it.
set myList to {}
set myItem to 1
copy myItem to end of myList
set item 1 of myList to 123
copy myItem to end of myList
MyList
--
Barry
_______________________________________________
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