How come I can set a bunch of variables using a list like this:
set {variable1, variable2} to {1, 2}
But the following has no effect on the value of variable1?
set item 1 of {variable1, variable2} to 3
Does a list only contain a reference to the value of variables but not the variables themselves?
It’s not a reference to the value – it is the value.
The variables in the list are resolved to their values.
set variable1 to "A"
set variable2 to "B"
return {variable1, variable2}
--> {"A", "B"}
It’s possible to do what you’re attempting, but some abstraction is required:
set variable1 to "A"
set variable2 to "B"
set refList to {a reference to variable1, a reference to variable2}
set contents of item 1 of refList to 3
return contents of items of refList
--> {3, "B"}
3 Likes
Â
Yes, it is stored in RAM as references list, but is returned (retrieved) as values list.
If you want to change the contents of the reference item 1 of the list without (yet) fetching the value from memory, you must set it to the same data type (type “reference”). The following script is about your 2nd example. In it, the content of the reference item 1 of the list is changed in the penultimate line of code.
Â
set var1 to "A"
set var2 to "B"
set aList to {var1, var2}
set item 1 of aList to a reference to 3 -- replace in the RAM
return aList --> {3,"B'} -- retrieved values
Â
Regarding your 1st example. Since every list (the list {1,2} bellow too) is stored in memory as a list of references, you can also do this (“modify on the fly”, without retrieving yet).:
Â
set var1 to "A"
set var2 to "B"
set aList to {var1, var2}
set aList to {1, 2} -- replace in the RAM
return aList --> {1,2} -- get values
Â
You can replace this way as well:Â
set var1 to "A"
set var2 to "B"
set {var1, var2} to {1, 2} -- replace in RAM
return {var1, var2} -- get values
Â
2 Likes