Count letters: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(Redirect to Letter frequency task)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
#REDIRECT [[Letter frequency]]
{{task|Count letters}}

;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

ασδ ξκλ ασδ ξα

<br><br>

=={{header|langur}}==
{{works with|langur|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|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>

{{out}}
<pre>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}</pre>

Latest revision as of 10:01, 12 March 2020

Redirect to: