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.

Comparing 2 scrambled words

Old posts that have not been replied to for several years.
Locked
User avatar
z_one
Master
Posts: 269
Joined: Mon Jan 14, 2002 8:00 pm
Location: Canada

Post by z_one »

Is there a way to compare two strings of same length to see if they are made of the same characters ?

Example: "hello" and "lleho"

If yes what would be the tcl code to do that ?

Thanks
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Code: Select all

set scramble1 "hlleo"
set scramble2 "loehl"
if {[join [lsort [split $scramble1 {}]] {}] == [join [lsort [split $scramble2 {}]] {}]} {
Words match
} else {
words do not match
}
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

You will need to convert the 2 words to lower or uppercase first, as the match is case sansative.
User avatar
z_one
Master
Posts: 269
Joined: Mon Jan 14, 2002 8:00 pm
Location: Canada

Post by z_one »

Thx revered one .. oops I mean ppslim :smile:

<font size=-1>[ This Message was edited by: z_one on 2002-05-20 11:01 ]</font>
J
Jules_74

Post by Jules_74 »

Why not use string compare as it's faster and you can use the -nocase flag since tcl 8.3 so you won't need to convert to upper- or lowercase first...
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

you try coding it and then you'll discover why it can't be used :razz:
J
Jules_74

Post by Jules_74 »

Already have... simply works... no problems... Maybe I'm thick but I can't see the problem...
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

[11:34] <Petersen> .tcl string compare abc cba
[11:34] <gaynewf> TCL: -1
[11:34] <Petersen> .tcl string compare bac abc
[11:34] <gaynewf> TCL: 1
[11:35] <Petersen> .tcl string compare abc abc
[11:35] <gaynewf> TCL: 0

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)
J
Jules_74

Post by Jules_74 »

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.
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

% 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.
Locked