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 split the result

Help for those learning Tcl or writing their own scripts.
Post Reply
R
Reynaldo
Halfop
Posts: 54
Joined: Wed May 11, 2005 2:51 am

Help split the result

Post by Reynaldo »

i've already grab from web into $result

Code: Select all

foreach line [split $result "\n"] {
	puthlp "PRIVMSG $nick :$notc $line"
	}
but the problem is, some lines in $result is to long. how to make it about max 200 words each lines?.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
R
Reynaldo
Halfop
Posts: 54
Joined: Wed May 11, 2005 2:51 am

Post by Reynaldo »

after parse the html into $word

Code: Select all


    set output "$wordwrap $word"
    foreach x [channels] {
    putserv "PRIVMSG $x :$output"  
}
proc wordwrap {str {len 70} {splitChr { }}} { 
   set out [set cur {}]; set i 0 
   foreach word [split [set str][unset str] $splitChr] { 
      if {[incr i [string len $word]]>$len} { 
         lappend out [join $cur $splitChr] 
         set cur [list $word] 
         set i [string len $word] 
      } { 
         lappend cur $word 
      } 
      incr i 
   } 
   lappend out [join $cur $splitChr] 
} 
seems it's not working :cry:
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

set output "$wordwrap $word"
Did you mean

Code: Select all

set output [wordwrap $word]
?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

btw. if server supports multitargeting of messages I would suggest to use:
[join [channels] ,]
as target :D.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
R
Reynaldo
Halfop
Posts: 54
Joined: Wed May 11, 2005 2:51 am

Post by Reynaldo »

Code: Select all

foreach line [wordwrap $word] { 
   puthlp "PRIVMSG $chan :$line" 
}
i got it! :lol:
Thank you Guys :shock:
R
Reynaldo
Halfop
Posts: 54
Joined: Wed May 11, 2005 2:51 am

Post by Reynaldo »

There is another problem, message send, too fast, the bot always quit (excess flood). any idea? :cry: i set the char to 250 :wink:
how to make it delay 5seconds each line?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

Reynaldo wrote:There is another problem, message send, too fast, the bot always quit (excess flood). any idea? :cry: i set the char to 250 :wink:
how to make it delay 5seconds each line?
1st:
"PRIVMSG $chan :" is at least 12 characters long, depending on channel maybe even 20. So I'd advise to wrap at around 230-235 chars, if want to keep line size below 256 bytes.
2nd:
if your bot floods on putserv or even on puthelp, you should consider increasing '#define msgrate' in your 'src/mod/server.mod/server.c' (and of course recompile and reinstall). This will however slow down reaction speed of the bot, but better 1s later than ending in being kicked.
Default and advised value is 2, my bots run well with 1 on QuakeNet and EuIRCnet. But might need to be set to 3 or 4 on more strict networks and depending on expected line length (1 assumes you do not exceed 250 characters/line).
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

some enhancement for splitting long lines

Post by rosc2112 »

I did a little enhancement to this word-wrapper, thought I'd share :)

Code: Select all

############################################################################################################
# If the input came from a msg bind, we set $chan to "privmsg" 
# for example:
bind msg - !mytrigger myproc
proc mytrigger {nick uhost hand text} {
        # pass the params to 'wrapproc' and set the channelname to "privmsg"
        wrapproc $nick $uhost $hand privmsg $text
        return
}

bind pub - !mytrigger wrapproc

proc wrapproc {nick uhost hand chan text} {
        if {$chan == "privmsg"} {set chan $nick}

        # inputtitle would normally be the commandline given with the trigger, such as '!horoscope leo' 
        # title would be set to 'leo'
        # You'd have to do further processing on the $text input of course, to limit its length, 
        # split it to make it tcl-special char safe, etc.
        if {$text != ""} {set inputtitle "$text"} else {set inputtitle "titlefoo"}

        # here we grab data into a var, say $sometext, from something like a geturl/regexp operation
        set sometext "some real long string of text over 400 chars for example"

        # word wrapper
        set j 0
        set linecount 0
        foreach line [split $sometext \n] {
                if {$line != ""} {
                        set len 375
                        set splitChr " "
                        set out [set cur {}]; set i 0
                        foreach word [split $line $splitChr] {
                                if {[incr i [string len $word]]>$len} {
                                        lappend out [join $cur $splitChr]
                                        set cur [list $word]
                                        set i [string len $word]
                                        incr j
                                } else {
                                        lappend cur $word
                                }
                                incr i
                        }
                        lappend out [join $cur $splitChr]
                        foreach line2 $out {
                                if {$linecount == 0} {
                                        set line2 [linsert $line2 0 \002[string totitle $inputtitle]\002:]
                                        incr linecount
                                        if {$j >= 1} {
                                                set line2 [linsert $line2 end \002(con't)\002]
                                                set j [expr $j - 1]
                                        }
                                } else {
                                        set line2 [linsert $line2 0 \002($inputtitle con't)\002]
                                        if {$j >= 1} {
                                                set line2 [linsert $line2 end \002(con't)\002]
                                                set j [expr $j - 1]
                                        }
                                }
                                puthelp "PRIVMSG $chan :$line2"
                        }
                }
        }
        puthelp "PRIVMSG $chan :\[end of $inputtitle output.\]"
}

I've been using this for various scripts, like the dream and funny horoscope scripts I made. The final result looks something like:

<TheEntity> Taurus: Everything will suddenly sound good to you today. This may be due to a syringing appointment you have earlier in the week, or it may be become love has just entered your life in the form someone floating down a river in a large plastic swan-type boat. Or it may just be because you've won a serious amount of money and people want to shower you with platitudes. Speaking of (con't)
<TheEntity> (taurus con't) which - you do look amazing today! Parts of you feel like giving up the ghost today, but feeding those parts with ice-cream and bacon bits may revive them, to an extent. Clowns with pickaxes will haunt your dreams tonight and will affect you at around mid-morning the following day. The effect of such dreams will cause you to a) be tired, b) look startled when people slam (con't)
<TheEntity> (taurus con't) the door, or c) be nice to any clowns you meet.
<TheEntity> [end of taurus horoscope]
M
Merky
Voice
Posts: 17
Joined: Sun Jan 21, 2007 9:07 am

Post by Merky »

how about i split file.txt?

set sometext "some real long string of text over 400 chars for example"

set sometext "file.txt"
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Read the faq about basic file operations (reading/writing files) and use puts instead of puthelp of course.
Post Reply