Date: Tue Jan 5 07:14:32 MST 1999 From: James Newton <newton@planetb.fr> Subject: Lists as References
>I made a copy of a list and then deleted from the copy. Somehow D7 is >deleting from both lists.You have stumbled on one of the most powerful features of lists. It is not new to D7.
A list is a bit like a map: it shows you where things are, without you actually having to be there. You can run your finger across a map in much less time than it takes to travel the same distance for real.
In fact, a list is more like a magic map. Imagine being able to draw a tiny dot on a world map... and that suddenly a whole new city came into existence. Rub out the dot and the city disappears. Imagine this too: you can make copies of your map and give them to other people. Whenever someone else creates a new city on their map, not only does the city suddenly exist, but it simultaneously appears on your map, and all the other maps too.
This is what happens when you copy a list from one variable to another. What is copied is the map, not the information the map represents. That information is stored in one well-defined place in RAM space. What the variable holds is a pointer to that information, not the information itself.
If one variable uses its pointer to the list to modify the information, all the other variable immediately find that the information that they have access to has changed.
To solve your immediate problem, use
set list2 = duplicate (list1)
This creates a new list containing the same information, but with no
link to the original.
Rects and points are also lists. Try this:
set point1 = point (0, 0)
set point2 = point1
set the locH of point1 = 20
put point2
-- point(20, 0)
setAt point2, 2, #locV
put point1
-- point(20, #locV)
(The last example is for demonstration purposes only. I cannot think
of any reason why you would want to do this in practice).
Hope this helps,
James
-- James Newton Tel: + 33 3 80 75 16 69 F - 21120 Vernot Fax: + 33 3 80 75 16 71 France Email: newton@planetb.fr