Mysqltcl foreach循环tcl

时间:2013-03-05 06:13:33

标签: mysql loops foreach tcl eggdrop

我的问题是第一个set返回会丢弃$ sub1,然后才能在字符串匹配中使用它,因此脚本不会继续。我试图将其余的脚本包含在第一个集合返回中,但是它可以工作但是...我向用户发送了多条消息,而其他两个集合的通道返回。

无论如何要修复它,以便它不会向用户和频道发送多条消息 因为“foreach sub”中的每个sub为pm用户和pm通道生成一条线,根据数据库中的匹配数量,它可以从1或2条消息到200条消息。

bind pubm - * bind_pubm
    proc bind_pubm {nick uhost handle channel text} {
        global SQL; sql:start

        set regexp {^!sub (.*) *$}
        if {[regexp -nocase -- $regexp $text -> subscription]} {
            if {[string match -nocase *HDTV* $subscription] || [string match -nocase *S??E??* $subscription]} {

            set return [mysqlsel $SQL(conn) "select sub from DB_Subs where category='TV'" -list] 
            foreach {sub} $return {     ; # Lists all the subs in the DB for matching.
            regsub -all -- {%} $sub "*" sub1    ; # Replaces % in SQL to * for the String match later.
            }

            if {[string match -nocase $sub1* $subscription]} {  ; # Matches if a sub in the previous list matches the regexp subscription fromthe beginging of proc.
                set return [mysqlsel $SQL(conn) "select user from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -list]
                    foreach line $return {
                        foreach {user} $line break
                            if {[onchan $user $beta]} {     ; # If the user is on the channel it PM each user.
                                putnow "PRIVMSG $nick : Subscription found matching your request."
                            } else {
                        }
                    }

            set return [mysqlsel $SQL(conn) "select count(DISTINCT user) from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -flatlist] ; # Counts the users for the Channel message.
                foreach {users} $return break
                    putnow "PRIVMSG $beta :SUBSCRIPTION: $users Users With Subscriptions for $subscription"
            } else {
            }
        } else {
        }
    }
}

很难解释我想要完成什么。

最终我试图达到的结果是......

  • 从正则表达式设置$ subscription
  • 列出数据库中的所有潜艇
  • 将DB中sub中的所有%转换为*以进行以下匹配
  • 尝试将子匹配到$ subscription
  • 如果匹配则继续下一个SELECT
  • 从数据库中选择所有“USERS”,其中子类似于%sub%
  • 然后向每个用户发送与select
  • 匹配的消息
  • 上次设置返回计算与选择匹配的用户数并将消息发送到频道

使用Donal建议的解决方案后。似乎所有事情都表现得像一个小问题。 [string match -nocase [get_subscription $ SQL(conn)] * $ subscription]部分代码不会将每个保存为要检查的变量。哪个行首先显示它使用而停止而不是完成列表以查看是否还有更多匹配。某些条目以不同方式添加,但应提供相同的结果。例如,一些条目被添加为The.TV.Show.S01或%TV%Show%S01 这意味着它应匹配两个部分并获得准确的计数和用户。

1 个答案:

答案 0 :(得分:1)

这可能很难,因为你有太多的东西。尝试将代码分解为执行明确定义任务的较小部分。这称为重构,它是使代码易于理解的重要部分。

以下是一些建议的重构:

proc get_subscription {conn} {
    set return [mysqlsel $conn "select sub from DB_Subs where category='TV'" -list] 
    foreach {sub} $return {
        regsub -all -- {%} $sub "*" sub1
    }
    return $sub1
}

proc notify_subscription_user {conn nick sub beta} {
    set return [mysqlsel $conn "select user from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -list]
    foreach line $return {
        lassign $line user
        if {[onchan $user $beta]} {
            putnow "PRIVMSG $nick : Subscription found matching your request."
        }
    }
}

proc send_subscription_message {conn sub beta subscription_text} {
    set return [mysqlsel $conn "select count(DISTINCT user) from DB_Subs where sub LIKE '[mysqlescape $sub]%' AND category='TV'" -flatlist]
    lassign $return users
    putnow "PRIVMSG $beta :SUBSCRIPTION: $users Users With Subscriptions for $subscription_text"
}

通过这些,我们可以将其余代码重写为这样(删除空的else子句,将表达式拆分为行,组合嵌套的if测试;所有基本的东西):

bind pubm - * bind_pubm
proc bind_pubm {nick uhost handle channel text} {
    global SQL; sql:start

    if {
        [regexp -nocase -- {^!sub (.*) *$} $text -> subscription]
        && ([string match -nocase *HDTV* $subscription]
            || [string match -nocase *S??E??* $subscription])
        && [string match -nocase [get_subscription $SQL(conn)]* $subscription]
    } then {
        notify_subscription_user $SQL(conn) $nick $sub $beta
        send_subscription_message $SQL(conn) $sub $beta $subscription
    }
}

这可以解决您的问题吗?我不知道,但它应该给你一个更好的基础来开始。