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.

multiple IF

Help for those learning Tcl or writing their own scripts.
Post Reply
m
m4st3r
Voice
Posts: 5
Joined: Thu Sep 14, 2006 11:15 pm

multiple IF

Post by m4st3r »

Hello ;)

im working on a script, and i want to know if i can use list or something in IF cmd .. Example:
For the moment i use something like this:

Code: Select all

if {[string match *test* $checkname] == 0 && [string match *roger* $checkname] == 0 && [string match *mary* $checkname] == 0 && [string match *merlin* $checkname] == 0 && [string match *polly* $checkname] == 0 && [string match *mandie* $checkname] == 0 } { 
but i have ALOT of IF like this ... i want to know i i can use something like a "list" like:

Code: Select all

list = "*test*,*roger*,*mary*, ..... "
if {$checkname match $list ) { ....
Thanks
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Yes.

Code: Select all

set nicknames "name1 name2 name3 name4"

if {[lsearch -exact $nicknames $checkname] != -1} {
     # named matched
} else {
     # not matched
}

Or use lsearch -glob for matching wildcards

if {[lsearch -glob $nicknames *$checkname*] != -1} {
     # etc
}
m
metroid
Owner
Posts: 771
Joined: Wed Jun 16, 2004 2:46 am

Post by metroid »

Also, since string match will only return 1 or 0, you can simply use

Code: Select all

if {![string match *test* $checkname] && ![string match *roger* $checkname] && ![string match *mary* $checkname] && ![string match *merlin* $checkname] && ![string match *polly* $checkname] && ![string match *mandie* $checkname]} {
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

rosc2112 wrote:Or use lsearch -glob for matching wildcards
His wildcards are in the list, not in the part matched against the list...
Something like this might do what he wants:

Code: Select all

switch -glob -- $checkname {
	*test* -
	*roger* -
	*mary* -
	*merlin* -
	*polly* -
	*mandie* {
		# match
	}
	default {
		# no match
	}
}
Have you ever read "The Manual"?
Post Reply