Re: copy & set statments
Re: copy & set statments
- Subject: Re: copy & set statments
- From: email@hidden
- Date: Sun, 21 Oct 2001 02:54:43 EDT
David,
You've discovered data sharing. Data sharing applies to lists, records, and
script objects. Consider it a documented "gotcha."
More knowledgeable people will correct me if I am wrong here, but basically
what happens is that in this case:
set x to 8
set y to x
is that the value held by the variable x is placed into the variable y.
However, when you do this:
set x to {"a","b"}
set y to x
you create a pointer to x's data and place it in y. Then, when x's data
changes, so does y's. I think this saves memory or some such pointless thing
(now that you can get a gig of RAM for a song). Personally, I've never really
found this to be useful, and have been stung by it myself.
On the other hand, this usage:
set x to {"a","b"}
copy x to y
actually places into y the values contained in x. The 2 lists can then be
separately manipulated.
Jeff Baumann
email@hidden
www.linkedresources.com
In a message dated 10/20/01 10:58:20 PM, David Dittmann wrote:
>
In this script at the end of execution:
>
s = 5
>
J = 2
>
n = 9
>
>
set n to 2
>
set s to 5
>
set j to n
>
set n to 9
>
>
My question relates to the behavior of J. In the script above J is the value
>
of what n was before N was changed. However, in the script below the
behavior
>
is much different. The behavior of the script below is different because the
>
variable theList is changed even though it is not directly changed by the
>
script. Why does nAList act like it is the variable theList even though
theList
>
>
is not being used in that example?
>
>
set theList to {{1, 2, 3, 4, 5}, {78, 79, 69, 36, 23}}
>
>
set newList to {}
>
set nAList to {}
>
>
repeat with l from 1 to count of items of theList
>
set newList to newList & item l of theList as list
>
--merge the list into a list that is not a list of lists
>
end repeat
>
>
>
-->lines of interest
>
set theList to newList
>
copy newList to nAList
>
-- but if the line above would read what the line below says-set nAList to
>
newList
>
--set nAList to newList
>
>
repeat with n from 1 to count of items of nAList
>
set sliced to "klk"
>
set item n of nAList to sliced
>
end repeat
>
>
return {nAList, theList}
>
>
(*--> result from the copy statement {{"klk", "klk", "klk", "klk", "klk",
"klk"
>
, "klk", "klk", "klk", "klk"}, {1, 2, 3, 4, 5, 78, 79, 69, 36, 23}}
>
>
--> result from the set statement {{"klk", "klk", "klk", "klk", "klk", "klk"
>
, "klk", "klk", "klk", "klk"}, {"klk", "klk", "klk", "klk", "klk", "klk",
"klk"
>
, "klk", "klk", "klk"}
>
*)
>
>
Have I had to much Mtn Dew, misunderstood the definition of the set
statement
>
or did I catch an undocumented gotcha?
>
>
>
scratching my head,
>
David Dittmann