[REBOL] Re: change/at
From: joel:neely:fedex at: 18-Sep-2002 13:09
Hi, Pat,
pat665 wrote:
> Hi List,
>
> I think I fall in a very common gotcha. The starting point was:
>
> test: [ false false false ]
> i: 2
>
> I wanted to change false to true for the ith position (
> [ false false false ]
> becoming
> [ false true false ]
> ).
> My first attemp was:
>
> test/:i: true
>
> The result was
>
> ** Syntax Error: Invalid word -- :i:
> ** Near: (line 1) test/:i: true
>
Very common indeed. I think most of us have been bitten by
that (or at least struggled to find a way to accomplish the
equivalent) somewhere along our respective REBOL paths.
The best solution I've found (in terms of consistency and speed,
but not necessarily in terms of obviousness ;-) is this:
>> test: [[0] [0] [0]] == [[0] [0] [0]]
>> i: 2 == 2
>> test/:i/1: 1 == [1]
>> test == [[0] [1] [0]]
>> i: i + 1 == 3
>> test/:i/1 == 0
>> test/:i/1: 2 == [2]
>> test == [[0] [1] [2]]
(I switched to numeric values to avoid having to discuss why
neither of the expressions
test: [false false false]
nor
test: [[false][false][false]]
contain LOGIC! values ;-)
The extra level of structure apparently costs less than the extra
words required to do PICK or POKE or CHANGE ... AT ... or any
other variation that I've found thusfar.
-jn-