Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Counting chars (was Simple things should be simple to do ?)

 [1/3] from: jeff:rebol at: 16-Nov-2000 10:19


Here's another two approaches that are fun: REBOL [] count: func [x /local count cur n1 n2][ sort x count: copy [] cur: first x n1: 1 forall x [ if any [all [tail? next x n1: n1 - 1] x/1 <> cur] [ repend count [cur (n2: index? x) - n1] n1: n2 cur: x/1 ] ] count ] str: "sdfsdfsdfsdfsdfsdfsdfsdf" probe count str count2: func [x /local count rule schar wchar][ rule: copy count: copy [] foreach char unique x [ do reduce [schar: to-set-word form char 0] wchar: to-word :schar append rule compose [(char) (to-paren reduce [:schar wchar '+ 1]) | ] repend count [char wchar] ] remove back tail rule parse x [some rule] reduce count ] probe count2 str

 [2/3] from: joel::neely::fedex::com at: 16-Nov-2000 17:22


Hello, List! Not losing sight of the fact that the original issue was about simple things being simple to do... For comparison, here is the same thing in Perl (no rock-throwing, please!). #!/usr/local/bin/perl -w use English; $test = "sdfsdfsdfsdfsdfsd"; %counts = (); foreach $char (split //, $test) {++$counts{$char}}; $OUTPUT_FIELD_SEPARATOR = " "; print %counts, "\n"; When this is run from the command line, one gets... # count-chars.pl s 6 d 6 f 5 Observations: 1) Splitting the string into characters is very easy. 2) Tallying the characters using an associative array (hash) is very easy. 3) Uninitialized values behave as zeroes for the relevant operator, making accumulation very easy. Not to offend, just to provoke some thought... -jn- -- ; Joel Neely [joel--neely--fedex--com] 901-263-4460 38017/HKA/9677 REBOL [] foreach [order string] sort/skip reduce [ true "!" false head reverse "rekcah" none "REBOL " prin "Just " "another " ] 2 [prin string] print ""

 [3/3] from: tooki:widebay:au at: 17-Nov-2000 14:57


Could you post that map thingie? Just out of curiosity? Your solution does have the economy of expression I like. Bard