RosettaCodeData/Task/Letter-frequency/Tcl/letter-frequency.tcl
Ingy döt Net db842d013d A-M baby
2013-04-10 21:29:02 -07:00

19 lines
528 B
Tcl

proc letterHistogram {fileName} {
# Initialize table (in case of short texts without every letter)
for {set i 97} {$i<=122} {incr i} {
set frequency([format %c $i]) 0
}
# Iterate over characters in file
set f [open $fileName]
foreach c [split [read $f] ""] {
# Count them if they're alphabetic
if {[string is alpha $c]} {
incr frequency([string tolower $c])
}
}
close $f
# Print the histogram
parray frequency
}
letterHistogram the/sample.txt