Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Strange array behavior

From: joel:neely:fedex at: 21-May-2002 17:29

Hi, Geza, Geza Lakner MD wrote:
> Hi REBOL-fellows! > > Why does not properly work this code segment: > > >> a: array/initial [3 2] [[]] > == [[[] []] [[] []] [[] []]] > >> append a/1/2 3 > == [3] > >> a > == [[[3] [3]] [[3] [3]] [[3] [3]]] >
It's working properly, just not intuitively! ;-) What's happening is that the initial value consists of a reference to an (initially) empty series. All of the elements of the new structure are initialized to that same reference, per the meaning of the /INITIAL refinement. However, since they are all referring to the same series, mutations of that series are visible through all of those references.
> Hmmm, I' ve expected at querying 'a : > == [[[] [3]] [[] []] [[] []]] > > Maybe the prototype initial value is a reference not a true value? > OK, I tried it even this way: > >> a: array/initial [3 2] copy [[]] > > Maybe that even 'copy has made only ONE copy and five references to it > in array 'a ? >
You got it!
> Now, if I want to manipulate arrays with "appendable" empty > lists, I don't give initial values and change each occurrances of > 'none to copy/deep [[]] and THEN append to the defined location. > > Is this the best practice to overcome the seeming limitation of > 'array ? >
How about this:
>> a: copy/deep array/initial [3 2] [[]]
== [[[] []] [[] []] [[] []]] which uses ARRAY/INITIAL to make a "prototype" structure, then lets COPY/DEEP make all of the blocks unique, after which:
>> append a/1/2 3
== [3]
>> a
== [[[] [3]] [[] []] [[] []]] -jn-