[REBOL] Re: Block/array manipulation
From: joel:neely:fedex at: 15-Oct-2001 21:28
Hi, again, Mat,
Joel Neely wrote:
> stuffstats: func [b [block!] /local result where] [
> result: copy []
> foreach item b [
> either found? where: find result item [
> change next where 1 + second where
> ][
> append result reduce [item 1]
> ]
> ]
> result
> ]
>
Of course, the above version only works if the original block
excludes just the right combination of small integers... :-/
This version corrects for that deficiency:
>> stuff: [1 2 3 2 1 4 1 2 3 1]
== [1 2 3 2 1 4 1 2 3 1]
stuffstats: func [b [block!] /local result where] [
result: copy []
foreach item b [
either found? where: find/skip result item 2 [
change next where 1 + second where
][
append result reduce [item 1]
]
]
result
]
as in
>> stuffstats stuff
== [1 4 2 3 3 2 4 1]
Finally, a version that avoids the data type issue and give the
results back in a nice order:
stuffstats: func [b [block!] /local c i j result] [
result: copy []
c: sort copy b
while [not tail? c] [
i: first c
j: 0
while [all [not tail? c i = first c]] [
j: j + 1
c: next c
]
append result reduce [i j]
]
result
]
... with both integers ...
>> stuffstats stuff
== [1 4 2 3 3 2 4 1]
... and strings.
>> stuff: ["fire" "water" "air" "water" "water" "air"]
== ["fire" "water" "air" "water" "water" "air"]
>> stuffstats stuff
== ["air" 2 "fire" 1 "water" 3]
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;