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.

multiples conditions, have better way?

Old posts that have not been replied to for several years.
Locked
User avatar
LorT
Voice
Posts: 15
Joined: Sun Mar 09, 2003 6:11 pm

multiples conditions, have better way?

Post by LorT »

i have 11 conditions in a proc.
if {[lindex $text 0] == "commands"} {
pub:learn:add $nick $chan[lrange $text 0 end]
return 0
}
if {[lindex $text 0] == "rosters"} {
pub:learn:add $nick $chan[lrange $text 0 end]
return 0
}
to......
if {[lindex $text 0] == "servers"} {
pub:learn:add $nick $chan[lrange $text 0 end]
return 0
}
puthelp "notice $nick :invalid option. optin: ............"

new i change it to
if {[lindex $text 0] == "commands" || [lindex $text 0] == "rosters" ......|| [lindex $text 0] == "servers"} {
pub:learn:add $nick $chan[lrange $text 0 end]
return 0
}
puthelp "notice $nick :invalid option. optin: ............"

have a better way to do if?

edit: change the string name
Last edited by LorT on Thu Mar 11, 2004 1:45 pm, edited 1 time in total.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

use [code][/code] around your code in the future

Post by user »

Code: Select all

if {[string match {[a-k]} [lindex $text 0]]} {
	whatever
}
Have you ever read "The Manual"?
User avatar
LorT
Voice
Posts: 15
Joined: Sun Mar 09, 2003 6:11 pm

Post by LorT »

sorry my bad
the string arent a-k is only ej.
the string are commands, roster, rules, records, servers....
how can i check if?
User avatar
Xpert
Halfop
Posts: 88
Joined: Mon Mar 08, 2004 7:03 am

Post by Xpert »

Code: Select all

if {([lindex $text 0] == "commands") || ([lindex $text 0] == "rosters") || ([lindex $text 0] == "rules") || ([lindex $text 0] == "servers")} { 
  pub:learn:add $nick $chan[lrange $text 0 end] 
  return 0 
}
Don't forget the "(,)" if you have more than one condition :)
Xpert.
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Xpert : foo is not equal with FOO, FoO, fOO, etc. So, either use "string tolower" to lower the stuff you compare or use "string equal -nocase" Consult the manual about them.
Once the game is over, the king and the pawn go back in the same box.
User avatar
Xpert
Halfop
Posts: 88
Joined: Mon Mar 08, 2004 7:03 am

Post by Xpert »

Sorry, i forgot about the strlwr :P
Xpert.
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

When you are using "strlwr" you are actualy calling an proc from alltools.tcl that will do that "string tolower" thing and this will slow your proc down a bit :P
Once the game is over, the king and the pawn go back in the same box.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

Alternatively....

Code: Select all

if {[regexp -nocase -- {^commands$|^rosters$|^rules$|^servers$} [lindex $text 0]]} {
... do stuff ...
}
However, while the code is more concise, regexp is a bit of a cpu eater, so it may be a few microseconds slower than using direct comparitive expressions.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Four ways to do it

Post by user »

Code: Select all

% proc test find {
        foreach way {
                {set ok [expr {[lsearch -exact {word1 word2 word3 word4} $find]>-1}]}
                {switch -- $find {word1 - word2 - word3 - word4 {set ok 1} default {set ok 0}}}
                {set ok [expr {$find=="word1"||$find=="word2"||$find=="word3"||$find=="word4"}]}
                {set ok [regexp {^(word1|word2|word3|word4)$} $find]}
        } {
                puts "[time $way 10000] (result: [eval $way])"
        }
}
% test word1
4 microseconds per iteration (result: 1)
6 microseconds per iteration (result: 1)
7 microseconds per iteration (result: 1)
30 microseconds per iteration (result: 1)
% test word4
4 microseconds per iteration (result: 1)
6 microseconds per iteration (result: 1)
17 microseconds per iteration (result: 1)
30 microseconds per iteration (result: 1)
% test word12
4 microseconds per iteration (result: 0)
6 microseconds per iteration (result: 0)
17 microseconds per iteration (result: 0)
28 microseconds per iteration (result: 0)
Have you ever read "The Manual"?
Locked