set scramble1 "hlleo"
set scramble2 "loehl"
if {[join [lsort [split $scramble1 {}]] {}] == [join [lsort [split $scramble2 {}]] {}]} {
Words match
} else {
words do not match
}
do you see? string compare will not help you compare scrambled words, as all it does is check if the string on the left is less than (in a numerical sense) than the one on the right. The only time it ever returns true is in the third instance, where the strings are actually exactly the same (in which case if {$string1 == $string2} is a lot more efficient)
We want to know if the strings are equal, don't we? So that's the result we'd want. Basically using string equal would be better.
The problem is that the equality operator always tries to convert both
of the strings to integers, and if this fails, tries to convert both
into double, and if that fails, compares the strings as strings.
On the other hand, the [string foo] procedures are not compiled by tcl8!
So you loose performance with both. Nevertheless, [string compare ...]
is better than == in most cases, and [string length ...] always wins
over == "".
string compare doesn't compare in a numerical sense but in a lexicographical sense. see the man pages:
string compare ?-nocase? ?-length int? string1 string2
Perform a character-by-character comparison of strings string1 and string2. Returns -1, 0, or 1, depending on whether string1 is lexicographically less than, equal to, or greater than string2. If -length is specified, then only the first length characters are used in the comparison. If -length is negative, it is ignored. If -nocase is specified, then the strings are compared in a case-insensitive manner.
% time {if {"a" == "a"} {}} 1000
12 microseconds per iteration
% time {if {![string compare "a" "a"]} {}} 1000
13 microseconds per iteration
string compare is slower performance wise than a simple ==, though if you have to throw in case exemption then its slightly faster. However, it isn't portable to lower tcl vers if you do that.