Jump to content

Letter frequency: Difference between revisions

Line 5,555:
=={{header|Smalltalk}}==
Make it a bag of characters and get the counts:
{{works with|Smalltalk/X}}
<lang smalltalk>bagOfChars := 'someFile' asFilename contentsAsString asBag.
bag sortedCounts
select:[:assoc | assoc value isLetter ]
thenDo:[:assoc | assoc printCR].</lang>
If the file is huge, you may not want to read it in as a big string first, but directly feed the chars into the bag:
<lang smalltalk>bagOfChars := Bag new.
'someFile' asFilename readingLinesDo:[:eachLine | bagOfChars addAll:eachLine].
bag sortedCounts ...</lang>
 
To show all counts, replace the "select:thenDo:" by a simple "do:", as in:
<lang smalltalk>bag sortedCounts do:[:assoc | assoc printCR].</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.