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.

need to split a line if too long

Old posts that have not been replied to for several years.
Locked
S
ShavdApe
Halfop
Posts: 46
Joined: Mon Dec 15, 2003 5:22 pm

need to split a line if too long

Post by ShavdApe »

I have a bunch of words stored in a mysql database that I want to list but the line is too long

Code: Select all


		foreach row [sql "SELECT crapword FROM crapwords"] {
			incr wordcount 1
			append wordlist [lindex $row 0] " "    	
		}
		putquick "PRIVMSG $chan :Total Crapwords: $wordcount"

		putquick "PRIVMSG $chan : $wordlist "
anyway i can split the line once it reaches a certain size?
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Something like this should work

Code: Select all

proc cutlen {str len} {
if {$str == ""} {return $str}
set strlist ""
while {[string length $str] > $len} {
 set line ""
 foreach word $str {
   set line [lappend line $word]
   if {[string length $line] >= $len} {
     set strlist [lappend strlist $line]
     set strpos [expr [string length $line] + 1]
     set str [string range $str $strpos end]
     break
   }
 }
}
set strlist [lappend strlist $str]
}
then just call it with this code
and just change 75 to how many chars you want on each line

Code: Select all

set cutlines [cutlen $var 75]
foreach cutline $cutlines {
  putserv "PRIVMSG ......"
}
Hope this helps
S
ShavdApe
Halfop
Posts: 46
Joined: Mon Dec 15, 2003 5:22 pm

Post by ShavdApe »

Doesnt seem to work

I tried reducing the length to 5 and it didnt work

Code: Select all

............
		foreach row [sql "SELECT crapword FROM crapwords"] {
			incr wordcount 1
			append wordlist [lindex $row 0] " "    	
		}
		putquick "PRIVMSG $chan : Total CrapFilter words: $wordcount"
		putlog " Count $wordcount "
		set string $wordlist 
set cutlines [cutlen $string 75] 
foreach cutline $cutlines { 
  putserv "PRIVMSG $chan :$cutlines " 
}

	}
}

proc cutlen {str len} { 
if {$str == ""} {return $str} 
set strlist "" 
while {[string length $str] > $len} { 
 set line "" 
 foreach word $str { 
   set line [lappend line $word] 
   if {[string length $line] >= $len} { 
     set strlist [lappend strlist $line] 
     set strpos [expr [string length $line] + 1] 
     set str [string range $str $strpos end] 
     break 
   } 
 } 
} 
set strlist [lappend strlist $str] 
}
I tried to reduce the length to 20 and no I still got the same line and not split over multiple lines :(
Left it at 75 same result and no multiple line

It does seem to have done something its added { } around the string that matches the length.
I would split using } but I have that charachter in the list of words so I would run into a problem there I guess.
Last edited by ShavdApe on Sat May 29, 2004 3:23 pm, edited 1 time in total.
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

i guess i was a bit fast earlier, it wont cut words in two

here its set to 2
<gb> .t set cs [cutlen "aaa bb cc d" 2]; foreach c $cs { putlog $c }
<ufo> aaa
<ufo> bb
<ufo> cc
<ufo> d
and with 4
<gb> .t set cs [cutlen "aaa bb cc d" 4]; foreach c $cs { putlog $c }
<ufo> [21:31] aaa bb
<ufo> [21:31] cc d
...
See if this works
set cutlines [cutlen [split $list] 75]
foreach cutline $cutlines {
putserv "PRIVMSG ......"
}
S
ShavdApe
Halfop
Posts: 46
Joined: Mon Dec 15, 2003 5:22 pm

Post by ShavdApe »

YES YES YES
thankyou thankyou thankyou thankyou lol

been geting on my nerves for so so long
J
Jagg
Halfop
Posts: 53
Joined: Sat Jan 24, 2004 11:32 am

Post by Jagg »

Also from me a BIG THANKS for that code!!! 8)
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

FYI, the wordwrap proc I posted here is about 3-10 times faster (depending on the data). (it also tries to keep lines below the max whenever possible without splitting words.)
Have you ever read "The Manual"?
Locked