[REBOL] byte frequencies
From: joel:neely:fedex at: 5-Jul-2001 11:12
Hello, all!
WRT to the issue of tallying occurrences of byte values in
a file, the best I've thought of thus far is
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]
]
]
]
(except for the obvious minor refactoring of using an
additional temporary variable or two...)
Applied to its own source, it gives
>> bytefreq %bytefreq.r
#"^/" 16
#" " 105
#"!" 2
#"#" 1
#"+" 5
#"-" 2
#"/" 6
#"0" 3
#"1" 6
#"2" 2
#"5" 3
#"6" 1
#":" 2
#"<" 1
#"B" 1
#"E" 1
#"L" 1
#"O" 1
#"R" 1
#"[" 8
#"]" 8
#"a" 9
#"b" 4
#"c" 16
#"d" 2
#"e" 8
#"f" 15
#"h" 10
#"i" 13
#"k" 4
#"l" 9
#"m" 1
#"n" 4
#"o" 9
#"p" 5
#"q" 1
#"r" 10
#"s" 1
#"t" 12
#"u" 2
#"y" 2
Any suggestions for improvement?
-jn-
--
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com