0

I want match search from a list. I have dir names example:

blabla.aa
cc.oiwerwer
asfd.Dd.asoiwer

and I want to check if it is in the list (upper case should be ignored).

bind pub "-|-" !tt tt
proc tt {nick host handle channel arg} {

    set name [lindex [split $arg] 0]
    set groups {aa BB Cc DD Ee Ff gg hh}

    if {[lsearch -inline $groups $name] != -1} {
        putnow "PRIVMSG $channel :match name $name"
    }
}

No matter what I write, it always says match...

Regards

4

4 回答 4

1

If I understood correctly, you want to know if any element of the list groups matches the dir name examples. If that's so, then you should use a loop with string match:

bind pub "-|-" !tt tt
proc tt {nick host handle channel arg} {
    set name [lindex [split $arg] 0]
    set groups {aa BB Cc DD Ee Ff gg hh}

    foreach group $groups {
        if {[string match -nocase *$group* $name]} {
            putnow "PRIVMSG $channel :$name matched $group"
            break
        }
    }
}

codepad test

于 2019-05-13T18:31:48.103 回答
0

You specified the "-inline" parameter to lsearch. It returns the match or empty string. So, it is always doesn't equal to -1. Try to remove the "-inline" parameter. Also, probably you want to use the "-exact" parameter.

Reference: https://www.tcl.tk/man/tcl8.6/TclCmd/lsearch.htm

于 2019-05-13T18:13:16.893 回答
0

If you can arrange for your list of things to be all in one case (e.g., lower case) then you can use [string tolower] and the in operator to do the search. This is simpler than lsearch as it produces a clean binary result:

proc tt {nick host handle channel arg} {
    set name [lindex [split $arg] 0]
    set groups {aa bb cc dd ee ff gg hh}

    if {[string tolower $name] in $groups} {
        putnow "PRIVMSG $channel :match name $name"
    }
}
于 2019-05-14T07:35:17.610 回答
0

Your question is a bit unclear, but piecing together some clues, you might want:

set channels {
    blabla.aa
    cc.oiwerwer
    asfd.Dd.asoiwer
}
set groups {aa BB Cc DD Ee Ff gg hh}

foreach group $groups {
    set idx [lsearch -nocase $channels "*$group*"]
    if {$idx != -1} {
        puts "$group -> [lindex $channels $idx]"
    }
}

which outputs

aa -> blabla.aa
Cc -> cc.oiwerwer
DD -> asfd.Dd.asoiwer

Or, much more terse is:

lsearch -inline -all -nocase -regexp $channels [join $groups |]
blabla.aa cc.oiwerwer asfd.Dd.asoiwer
于 2019-05-14T16:49:42.040 回答