Fw: How to use a word instead of a numeric value
[1/3] 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
]
[2/3] from: antonr::lexicon::net at: 20-Jun-2005 14:37
Convert grouping to integer.
This should point out the problem:
>> copy/part "bonjour" 3.0
** Script Error: Invalid /part count: 3.0
** Near: copy/part "bonjour" 3.0
>> copy/part "bonjour" 3
== "bon"
Anton.
[3/3] from: gerardcote::sympatico::ca at: 20-Jun-2005 15:35
Thanks Anton,
It did the job and also answered a similar question I had about the "For" iterative construct
when I used it with a calculated word
constants, a bit as in:
constant: 1 - 3
for c 10 1 constant [print c]
When worked out in a similar way before, sometimes I had to change the for construct
to a while construct to clear the non desired
behaviour. The For simply refused to work as it should (at least to my eyes) and showed
the kind of restrictions you pointed out
below.
Curiously however the example above worked flawlessly so mine was probably more complex
but I can't remember it.
Gerard