demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Fri Jul 08, 2005 2:12 pm Post subject: |
|
|
well, the arrays is Tcl are associative per se, i.e. able to store key/value pairs, not limited to integer type key/index as most compiled languages
here's what I had in mind:
| Code: |
[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
|
|
|