[REBOL] Re: manipulating values in blocks
From: greggirwin:mindspring at: 27-Nov-2002 13:13
Hi Hugues,
HM> I manage to manipulate a block like [1 17 23 36 49 58],
HM> representing limits off segments, ie, if i have 6 values in the
HM> block, it seems that the block contains 5 segments (1-17, 17-23, 23-36 ...)
HM> is there anyone who knows how to manipulate the block into getting
HM> start and end values of each segment ? I've tried, but don't know
HM> how to do this job.
If I understand you correctly, you want to say "give me the start and
end values of segment 2" and receive 17 and 23 in response.
You could write functions to do this, like so:
>> b: [1 17 23 36 49 58]
== [1 17 23 36 49 58]
>> seg-start: func [block segment] [block/:segment]
>> seg-start b 3
== 23
>> seg-end: func [block segment] [pick block add 1 segment]
>> seg-end b 3
== 36
or you could extract a "segment" and then use that:
>> segment: func [block segment] [copy/part at block segment 2]
>> segment b 3
== [23 36]
and then use FIRST and LAST to get each value. You can also use these
techniques inline if that suits your needs better. Hmmm. You could
also "segmentize" your block if you wanted.
>> segmentize: func [block /local result][
[ result: make block! length? block
[ repeat i subtract length? block 1 [
[ append/only result copy/part at block i 2
[ ]
[ result
[ ]
>> segmentize b
== [[1 17] [17 23] [23 36] [36 49] [49 58]]
HTH!
-- Gregg