Count letters

From Rosetta Code
Revision as of 17:56, 11 March 2020 by Langurmonkey (talk | contribs)
Task
Count letters
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Count the letters used in a sentence, by code point, generating a hash or associative array, or appropriate construct for the language.

Use the following. The Greek letters are random and don't spell anything that I know of.

fuzzy furry kittens

ασδ ξκλ ασδ ξα



langur

Works with: langur version 0.7.0

<lang langur>var .countLetters = f(.s) {

   for[=h{}] .s2 in split(replace(.s, RE/\P{L}/)) {
       _for[.s2; 0] += 1
   }

}

writeln .countLetters("fuzzy furry kittens") writeln .countLetters("ασδ ξκλ ασδ ξα") # random Greek letters</lang>

Works with: langur version 0.6.11

<lang langur>var .countLetters = f(.s) {

   foldfrom(
       f(var .h2, .s2) { .h2[.s2; 0] += 1; .h2 },
       h{},
       split(replace(.s, RE/\P{L}/)),
   )

}</lang>

Output:
h{"f": 2, "u": 2, "z": 2, "y": 2, "r": 2, "k": 1, "i": 1, "t": 2, "e": 1, "n": 1, "s": 1}
h{"α": 3, "σ": 2, "δ": 2, "ξ": 2, "κ": 1, "λ": 1}