here's what I had in mind:
Code: Select all
[demond@whitepine demond]$ cat test.tcl
proc countwords str {
global count
set idx 0
while {1} {
set buf [string range $str $idx end]
if {[scan $buf %s word] > 0} {
incr idx [expr [string first $word $buf] + [string length $word]]
if {[info exists count($word)]} {incr count($word)} {set count($word) 1}
} {break}
}
}
[demond@whitepine demond]$ tclsh8.4
% source test.tcl
% set a "abc def \t123\n xyz\tabc\nabc 123\t"
abc def 123
xyz abc
abc 123
% countwords $a
% array get count
123 2 abc 3 xyz 1 def 1