At 06:19 AM 10/13/99 +0100, you wrote:
>How do I access Arrays ??
[snip]
>for i 1 3 1
>[
>    marray/i/i: 1
>]
>doesnt work ....

... and that is because array is not a type. Arrays are blocks. The word
array is a convenience function that implements blocks containing blocks.
The resulting block can be used like arrays, but there are a few notational
oddities that result from the fact that arrays are actually blocks.

Arrays are indexed using a path construct. Paths may also consist of
non-digit symbols:

>> my-array: [ i "This is the value of my-array at path position my-array/i"]
== [i {This is the value of my-array at path position my-array/i}]
>> my-array/i
== {This is the value of my-array at path position my-array/i}

When REBOL encounters your expression marray/i/:i it assumes that the first
i is a literal part of the path and does not look up its value. The second
i is replaced by its value because you are using the colon notation, :i.
This tells REBOL that you don't mean to use the the literal i as part of
the path.

>
>for i 1 3 1
>[
>    marray/:i/:i: 1
>]
>
>doesnt

Well, the error message kind of gives it away:

>> for i 1 3 1 [ my-array/:i/:i: 1 ]
** Syntax Error: Invalid word -- :i:.
** Where: (line 1) for i 1 3 1 [ my-array/:i/:i: 1 ]

REBOL does not like the construct :i:

You could use something like:

ar-poke: func [array [block!]
               index-list [block!]
               new-value 
               /local index at-location] [
  at-location: array  
  for i 1 (-1 + length? index-list) 2  [ 
    index: pick index-list i
    at-location: at-location/:index
  ]
  change at at-location last index-list new-value
]

Usage:

>> my-array: array/initial [3 3] 0
== [[0 0 0] [0 0 0] [0 0 0]]

>> ar-poke my-array [2 2] 3
== [0]
>> my-array
== [[0 0 0] [0 3 0] [0 0 0]]

>> ar-poke my-array [1 2] 5
== [0]
>> my-array
== [[0 5 0] [0 3 0] [0 0 0]]

Elan

Reply via email to