This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

SpamScan

Support & discussion of released scripts, and announcements of new releases.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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: 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
Post Reply