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.

Help about strings and lists

Help for those learning Tcl or writing their own scripts.
Post Reply
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Help about strings and lists

Post by cerberus_gr »

Hello,

I 'm writing a scrable game and I have some problem about strings and lists.

I have a mysql database where all questions and answers exist. I don't insert the questions myself, so maybe questions have invalid characters such as {, }, [, ].

The problem is where do I need to use [join], [concat],
  • commands in order not to have problems.

    A part of my code is the following

    Code: Select all

    # Comments:
    #  chid = channel's id from the database
    #  mysql::sel returns "{Album of John} {Hello world} 1 {Music}"
    #  gamebot:scrable:translate procedure needs to convert greek characters to english
    proc gamebot:scrable:round:new { chid } {
        global gamebot gamebot_scrable
        
        # Variables
        incr gamebot_scrable(round_$chid) 1
        
        # Read data from the database and creates the variables
        set query          "SELECT qhint,qanswer,cid,cname FROM questions,categories WHERE qchid=$chid"
        set data            [mysql::sel $gamebot(mysql) $query -flatlist]
        set hint            [gamebot:scrable:translate $chid [lindex $data 0]]
        set answer          [gamebot:scrable:translate $chid [lindex $data 1]]
        set categid         [lindex $data 2]
        set categname       [gamebot:scrable:translate $chid [lindex $data 3]]
    # ERROR OVER HERE :(
        #set answer_scrabled [gamebot:scrable_scrablefrase $answer]
        set gamebot_scrable(answer_$chid)  $answer
        set gamebot_scrable(categid_$chid) $categid
        set gamebot_scrable(qstart_$chid)  [clock clicks -milliseconds]
    
        # Creates the binds
        bind pubm - * gamebot:scrable_msg
    
        # Send the message to the channel
        gamebot:putmsg [gamebot:gettext $chid "SCRABLE_ROUND_NEW" "<round> $gamebot_scrable(round_$chid) <category> $categname <rounds> $gamebot_scrable(rounds_$chid)"]
        gamebot:putmsg [gamebot:gettext $chid "SCRABLE_HINT" "<hint> $hint"]
        gamebot:putmsg [gamebot:gettext $chid "SCRABLE_ANSWER" "<answer> $answer"]
    }
    
    
    proc gamebot:scrable:translate { chid args } {
        set args [string map {"Ου" "Ou" "Α" "A" "Ά" "A" "Β" "B" "Γ" "G" "Δ" "D" "Ε" "E" "Έ" "E" "Ζ" "Z" "Η" "I" "Ή" "I" "Θ" "Th" "Ι" "I" "Ί" "I" "Κ" "K" "Λ" "L" "Μ" "M" "Ν" "N" "Ξ" "Ks" "Ο" "O" "Ό" "O" "Π" "P" "Ρ" "R" "Σ" "S" "Τ" "T" "Υ" "Y" "Ύ" "Y" "Φ" "F" "Χ" "X" "Ψ" "Ps" "Ω" "O" "Ώ" "O"} $args]
        set args [string map {"ου" "ou" "α" "a" "ά" "a" "β" "b" "γ" "g" "δ" "d" "ε" "e" "έ" "e" "ζ" "z" "η" "i" "ή" "i" "θ" "th" "ι" "i" "ί" "i" "κ" "k" "λ" "l" "μ" "m" "ν" "n" "ξ" "ks" "ο" "o" "ό" "o" "π" "p" "ρ" "r" "σ" "s" "ς" "s" "τ" "t" "υ" "y" "ύ" "y" "φ" "f" "χ" "x" "ψ" "ps" "ω" "o" "ώ" "o"} $args]
        return $args
    }
    
    proc gamebot:scrable_scrableword { str } {
        # Variables
        set len [string length $str]
        set nstr ""
        
        while {$len > 0} {
            set num [format %.0f [expr {rand() * ($len - 1)}]]
            append nstr [string index $str $num]
            set str [string replace $str $num $num]
            incr len -1
        }
        
        return $nstr
    }
    
    
    proc gamebot:scrable_scrablefrase { str } {
        # Variables
        set words [regexp -all {\s} $str]
        set i 0
        set nstr ""
        
        while {$i <= $words} {
            lappend nstr [gamebot:srable_scrableword [join [lindex [split $str] $i]]]
            incr i
        }
        
        return [join $nstr]
    }
    
    Please help.
    Thanks
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

you won't have any problems and you don't need to strip those chars as long as you simply output db text and don't pass it to proc's which do evaluation
connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use

Code: Select all

 tag when posting logs, code
Post Reply