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.

String comparison

Old posts that have not been replied to for several years.
Locked
M
Massacre
Voice
Posts: 27
Joined: Wed Mar 19, 2003 4:17 pm

String comparison

Post by Massacre »

I just don't get it. I've spent the best part of 2 hours fiddling with this and I just can't see why it's not working. If I join our clan channel, #[ms], the following, which is in a proc that's bound to 'join' (and i know it works), results in false

Code: Select all

if {[string match -nocase $channel "#\[ms\]"]} {
or, from my debugging:

Code: Select all

putquick "PRIVMSG $channel :$channel [string match -nocase $channel "#\[ms\]"]"
gives the output: #[ms] 0

If i replace #[ms] with our private channel then join the private channel, the if results in true, but otherwise, it just doesn't work. It's not in any other if's, it's just there and it isn't working :S can anyone see something i'm doing wrong? i'd be very greatful!
M
Massacre
Voice
Posts: 27
Joined: Wed Mar 19, 2003 4:17 pm

Post by Massacre »

i've tried it with other channels there too, but it only failes on #[ms] :/
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

I believe [ ] has special meaning when used as a mask in string match. It means basically "any of the enclosed chars." Solution: use [string compare] instead.

% string match {[abc]} "a"
1
% string match {[abc]} "b"
1
% string match {[abc]} "e"
0
% string compare {[abc]} "a"
-1
% string compare {[abc]} {[abc]}
0 <-- correct
M
Massacre
Voice
Posts: 27
Joined: Wed Mar 19, 2003 4:17 pm

Post by Massacre »

Doesn't escaping (eg, '\[') get round that? And if not, do you or anyone else know how i can get round it?
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Escaping [ ] would get around it, but you didn't escape it (in the mask).

Using string compare like I said will get around it just fine.
M
Massacre
Voice
Posts: 27
Joined: Wed Mar 19, 2003 4:17 pm

Post by Massacre »

Isn't "#\[ms\]" excaping the []?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Evidently that will not. I did a test: .tcl putlog "#\[ms\]" and got as result: #[ms] Test yourself.. Oh and also it works if you use only one \ like: #\[ms]
Once the game is over, the king and the pawn go back in the same box.
M
Massacre
Voice
Posts: 27
Joined: Wed Mar 19, 2003 4:17 pm

Post by Massacre »

:s well i just tried:

Code: Select all

if {[string match -nocase $channel "#\[ms\]"]} {
putquick "PRIVMSG $channel :Test(1)"
}
if {[string match -nocase $channel "#\[ms]"]} {
putquick "PRIVMSG $channel :Test(2)"
}
and neither appeared in the channel. :/ The compare thing works though. Thnx for ur time guys.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Ok... the mask is $channel

The text is "#\[ms\]"

See, you escaped the [ ] in the text, but not the mask.

Solution: use "string compare" instead of "string match" (look it up in the man page) because it doesn't use a mask, and hence doesn't need to be escaped.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Read the Tcl man pages for the usage of the "string match" command.

it uses the format
string match [-nocase] <mask> <string>
The mask is the first item, not the second. Thus, because $channel isn't escaped, and is being passed as the mask potion, it is failing.
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

the best way to determine if two strings are equal is...drumroll... 'string equal' ..it even has a -nocase option :)
Locked