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

[REBOL] Re: Counting chars (was Simple things should be simple to do ?)

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 ""