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.

mysqltcl: replace string [Solved]

Help for those learning Tcl or writing their own scripts.
Post Reply
P
Pad
Voice
Posts: 3
Joined: Fri May 20, 2011 4:06 pm

mysqltcl: replace string [Solved]

Post by Pad »

Hi. I have an Eggdrop connecting to a MySQL database using mysqltcl and displaying the info on an IRC channel. Some rows contain text that I don't want to display like <br> and I want to omit it.

I tried to use the "string map" function ( http://wiki.tcl.tk/2819 ) but the bot crashed when it read the <br> on string map {<br> } $post. Then I tried to put only br to see if I was using it correctly but it seemed that the instruction was ignored.

I'm using this code to experiment:

Code: Select all

mysqlmap $db_handle {id username post date} {
  string map {br |} $post
  putserv "privmsg $chan :$post"
}
But it display the text normally. What am I doing wrong?
Last edited by Pad on Sat May 21, 2011 4:08 pm, edited 1 time in total.
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Re: mysqltcl: replace string

Post by speechles »

Code: Select all

mysqlmap $db_handle {id username post date} {
  set post [string map [list "<br>" " | "] $post]
  putserv "privmsg $chan :$post"
}
You use the string map to convert, but are never storing those results as a variable. I included the "set post" and bracket encapsulation [ ] around the [string map] to properly build the correct sequence of commands (stacking). I've also used [list "replace-this" "with-this"] which will work properly regarding variables replacements and special characters (ie, un-exploitable via errors or otherwise), as
  • is the preferred way to do [string map] rather than hand-crafting within curly braces { }.

    School is now in session. The bell has rang. Classroom is filling with students. Grab a seat while you can... ;)

    /me takes a bite of the apple on his desk.

    Having fun, is of tantamount importance as the task at hand. Remember this. Don't get angry, or give up when learning tcl. Have fun. Consider your failures as lessons, character building exercises. Not as some fundamental skill you lackl. Nobody is perfect. Having a laugh at stupid mistakes helps. Having fun while coding seriously is why I have a lot of success. For no other reason. I enjoy scripting for eggdrop. This is what keeps me sane. This is my wooden ship in a bottle, that I slowly build meticulously piece by piece over time.
Last edited by speechles on Fri May 20, 2011 6:22 pm, edited 1 time in total.
P
Pad
Voice
Posts: 3
Joined: Fri May 20, 2011 4:06 pm

Re: mysqltcl: replace string

Post by Pad »

It worked! Thank you! :D

One more thing: is there any special character in IRC to make a newline like '\n' for PHP, C, etc?
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

mysqlmap $db_handle {id username post date} {
  set post [string map [list "<br>" "\n"] $post]
  foreach line [split $post "\n"] {
    putserv "privmsg $chan :$line"
  }
}
This will show you how to encapsulate and render newlines. Try this one too.. ;)
P
Pad
Voice
Posts: 3
Joined: Fri May 20, 2011 4:06 pm

Post by Pad »

It worked too! Thank you very much! :D
Post Reply