Re: RE: copy & set statments
Re: RE: copy & set statments
- Subject: Re: RE: copy & set statments
- From: email@hidden
- Date: Mon, 22 Oct 2001 02:59:50 EDT
In a message dated 10/21/01 11:26:57 PM, email@hidden wrote:
>
The short of this is as follows:
>
>
a) 'copy' places a copy of the value into the variable:
>
>
copy x to 5 -- x gets 5
>
copy y to x -- y gets 5
Ouch! Check your syntax. Should be
copy 5 to x -- x gets 5
copy x to y -- y gets 5
>
b) 'set' places an address to the value (if possible) into the
>
variable. The exception to this is when you 'set' a variable to a
>
literal value, as in the 'set x to 5' line below:
>
>
Set x to 5 -- x gets 5
>
Set y to x -- y gets the address of x
>
-- if x changes, y sees the
new value
But if x changes, it points to a new memory location, while y still points to
the value which x used to have. In other words, y doesn't see x's change.
What you say is true only for lists and records (and script objects, with
some finagling).
>
c) 'a reference to' copies a reference (or address) into the variable.
>
This can be used for many object in AppleScript, such as files, items in
>
a list, words in a paragraph, etc.
>
>
set x to (a reference to word 2 of paragraph 3)
>
-- x gets 'word 2 of paragraph 3'
What? This looks like you are saying that x gets the literal value 'word 2 of
paragraph 3'! Assuming you are wrapping this in code which refers to actual
paragraph text, x will get the literal value of whatever that word is, and
will not change if the original text changes (someone provide code to prove
me wrong?).
>
copy (a reference to word 2 of paragraph 3) to x
>
-- x gets the actual value of word 2 of paragraph 3
Either one works identically, in practice.
>
-- note how copy's result was different because of its
>
nature
No, I don't note that.
>
set y to item 2 of theList
>
-- y gets 'item 2 of theList', if the item changes, y sees it
No, no, no! The variable y will get whatever value item 2 of theList has and
hold on to it, separate from anything that happens to theList. Try this code:
set theList to {"a", "b"}
set y to item 2 of theList
copy "c" to item 2 of theList -- or set item 2 of theList to "c"
log y
--> (*b*)
What you are saying will work in this semi-contrived scenario:
set theList to {"a", {"b"}}
set y to item 2 of theList
copy "c" to item 1 of item 2 of theList
log y
--> (*c*)
>
copy (a reference to item 2 of theList) to y
>
-- y gets the actual value of item 2 of theList
>
-- if item 2 changes, y doesn't see it
Then how do you explain this?
set theList to {"a", {"b"}}
copy (a reference to item 2 of theList) to y
copy "d" to item 1 of item 2 of theList
log y
--> (*d*)
>
Hope that helps.
Sorry, it didn't. You seem terribly confused about this subject. Look for
Nigel's explanation. I think he explained it best.
Jeff Baumann
email@hidden
www.linkedresources.com