[REBOL] Re: byte frequencies
From: joel:neely:fedex at: 5-Jul-2001 20:48
One other tidbit, Larry...
Larry Palmiter wrote:
> bytefreq2: func [fi [file!] /local ft cnt] [
> ft: array/initial 256 0
> foreach ch read fi [
> ch: 1 + ch
> poke ft ch 1 + pick ft ch
> ]
> repeat ch length? ft [
> if 0 < cnt: pick ft ch [
> print [mold to-char ch - 1 tab cnt]
> ]
> ]
> ]
>
...
> Your basic idea of using implicit type-conversion to allow
> characters to be used as indices...
>
>
> >
> > bytefreq: func [fi [file!] /local ft ch] [
> > ft: array/initial 256 0
> > foreach ch read to-file fi [
> > poke ft 1 + ch 1 + pick ft 1 + ch
> > ]
> > for ch 0 255 1 [
> > if 0 < pick ft 1 + ch [
> > print [mold to-char ch tab pick ft 1 + ch]
> > ]
> > ]
> > ]
> >
Over the years, I've developed the practice of using each
variable to mean exactly one thing (although, as with all
good intentions, I'm not perfect at it yet! ;-) I've often
found subtle bugs that result from redefining the meaning
or role of a variable "just a little bit".
Thus, my original is fairly obsessive about using CH to
mean "the current character I'm dealing with". That habit
explains my blind spot WRT flipping CH back-and-forth between
current character
and "index for current character". Of
course, in 0-origin indexing those two are the same, so there's
no arithmetic (or mental adjustment ;-) required.
Finally, to be consistent with your reminder to the reader
about int + char vs. char + int , I should note that the
loop
for ch 0 255 1 [...]
actually *had* to be written using INTEGER! bounds; if written
using CHAR! bounds, it never terminates!
Fair warning!
-jn-
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com