【问题标题】:Mysqltcl foreach loop tclmysqltcl foreach 循环 tcl
【发布时间】:2013-02-19 10:53:40
【问题描述】:

我的问题是第一组返回删除 $sub1 之前它可以用于字符串匹配,因此脚本不会继续。我已尝试将脚本的其余部分包含在第一组返回中,并且它可以工作,但是...我收到多条消息给用户和其他 2 组返回的通道。

无论如何都要修复它,使其不会向用户和频道发送多条消息 因为“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
  • 列出数据库中的所有子项
  • 将数据库中 sub 中的所有 % 转换为 * 以用于以下匹配
  • 尝试将 sub 与 $subscription 匹配
  • 如果匹配,则继续下一个 SELECT
  • 从数据库中选择所有“用户”,其中 sub 类似于 %sub%
  • 然后向每个用户发送一条匹配选择的消息
  • 最后一组返回计数匹配选择的用户数并向频道发送消息

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

【问题讨论】:

  • 如果难以解释您要完成的工作,请发布几个不同的简短问题,每个问题都针对一个可理解的问题。以目前的形式,我不确定如何处理您的问题。

标签: mysql loops foreach tcl eggdrop


【解决方案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
    }
}

这能解决您的问题吗?我不知道,但它应该会给你一个更好的起点。

【讨论】:

  • 看起来这个过程可以工作,但我遇到了 $sub 没有保持设置的问题,因为它可以在 notify_subscription_user 和 send_subscription_message 中使用
  • 另一个问题是订阅以各种格式保存在数据库中,有些可能是%tv%show 或 the.tv.show,所以我需要它不仅匹配一个
猜你喜欢
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 2016-09-26
  • 2012-07-19
  • 2016-10-08
相关资源
最近更新 更多