[REBOL] Re: Strange array behavior
From: ingo:2b1 at: 22-May-2002 0:34
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]]]
<..>
> 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
> ?
Or you could patch the array function to copy series values before
initializing, maybe like this:
array: func [
{Makes and initializes a series of a given size. *PATCHED* (iho)
copy/deep's series values, so you can use it to initialize with blocks}
size [integer! block!] "Size or block of sizes for each dimension"
/initial "Specify an initial value for all elements"
value "Initial value"
/local block rest
][
if not initial [value: none]
rest: none
if block? size [
rest: next size
if tail? rest [rest: none]
size: first size
if not integer? size [make error! "Integer size required"]
]
block: make block! size
either not rest [
; changes \/ \/ \/
either series? value [
loop size [ insert block copy/deep value ]
] [
insert/dup block value size
]
; changes /\ /\ /\
] [
loop size [
block: insert/only block array/initial rest value
]
]
head block
]
And after that:
>> a: array/initial [3 2] [[]]
== [[[] []] [[] []] [[] []]]
>> append a/1/2 3
== [3]
>> a
== [[[] [3]] [[] []] [[] []]]
I hope that helps,
Ingo