[REBOL] Fw: How to use a word instead of a numeric value
From: gerardcote::sympatico::ca at: 17-Jun-2005 20:57
Hello list,
I need some help here since I can't work the copy/part correctly when using "words" instead
of numbers.
The problem I'm working on:
-----------------------------
While working on a mimicked rebol version of the awk or C "printf" instruction somewhere
I have to convert binary numbers in
octal, decimal and hexadecimal.
During this process I have to assemble binary digits into chunks of 3 or 4. This is what
the following code
does - here for chunks of 3 since the code part for the hexa is done separately for now.
The code also pads to the left with 0
when needed, that is when Value length is not a multiple of 3.
Example: Value: "12345678" -> first padded to "012345678" and sliced to 678 345 012
Eventually the numbers will be 0 and 1 but for testing it is easier to use decimal numbers
to keep positions aligned.
So the code below works well for a binary to octal assembly - at least for some samples.
The difficulty I have is the following :
the copy/part doesn't accept the fact that if replace the last parameter (number 3) by
its equivalent valued word "grouping" which
is defined as number 3 (defined as grouping: 3).
I've tried to compose the complete line and do it after but this never worked for the
entire script. However this worked well on
smaller oneliner examples. I don't know why and can't explain this kind of anomaly (from
my beginnners eyes at least).
Can someone help ? Thanks in advance
Gerard
value: "12345678"
val-length: length? value
grouping: log-2 dest-base ; Since source-base is 2 (defined elsewhere)
reste: val-length // grouping ; Length of padding based on this
value
if reste <> 0 [
insert/dup value "0" (grouping - reste) ; Left padding done here
head value
]
c: length? value
while [c >= grouping] [
slice: copy ""
slice: copy/part at value (c - grouping + 1) 3 ; <- This 3 is to be replaced with
the word
print ["slice: " slice] ; "grouping"
but REBOL seems to reject it!
; remove/part at value c - 3 3
c: c - grouping
]